-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cbb4be4
commit 1c35ea4
Showing
186 changed files
with
13,009 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#------------------------------------------------- | ||
# | ||
# Project created by QtCreator 2014-11-21T20:00:31 | ||
# | ||
#------------------------------------------------- | ||
|
||
QT += core gui | ||
|
||
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets | ||
|
||
TARGET = CityModel | ||
TEMPLATE = app | ||
|
||
|
||
SOURCES += main.cpp\ | ||
citymodel.cpp | ||
|
||
HEADERS += citymodel.h | ||
|
||
FORMS += citymodel.ui |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#include "citymodel.h" | ||
#include "ui_citymodel.h" | ||
|
||
CityModel::CityModel(QObject *parent) : | ||
QAbstractTableModel(parent), | ||
ui(new Ui::CityModel) | ||
{ | ||
} | ||
|
||
void CityModel::setCities(const QStringList &cityNames) | ||
{ | ||
cities = cityNames; | ||
//用于存储城市之间的距离 | ||
distances.resize(cities.count() * (cities.count() - 1) / 2); | ||
distances.fill(0); | ||
} | ||
|
||
int CityModel::rowCount(const QModelIndex & /* parent */) const | ||
{ | ||
return cities.count(); | ||
} | ||
|
||
int CityModel::columnCount(const QModelIndex & /* parent */) const | ||
{ | ||
return cities.count(); | ||
} | ||
|
||
QVariant CityModel::data(const QModelIndex &index, int role) const | ||
{ | ||
if (!index.isValid()) | ||
return QVariant(); | ||
|
||
//当为对其模式的时候,则对其table表 | ||
if (role == Qt::TextAlignmentRole) { | ||
return int(Qt::AlignRight | Qt::AlignVCenter); | ||
} else if (role == Qt::DisplayRole) { | ||
//为同一个城市,则距离为0 | ||
if (index.row() == index.column()) | ||
return 0; | ||
//得到不同城市之间的距离 | ||
int offset = offsetOf(index.row(), index.column()); | ||
return distances[offset]; | ||
} | ||
return QVariant(); | ||
} | ||
|
||
//用户编辑一项的时候,会触发setData | ||
bool CityModel::setData(const QModelIndex &index, | ||
const QVariant &value, int role) | ||
{ | ||
if (index.isValid() && index.row() != index.column() | ||
&& role == Qt::EditRole) { | ||
int offset = offsetOf(index.row(), index.column()); | ||
distances[offset] = value.toInt(); | ||
|
||
//createIndex()函数用于产生一个模型索引。我们需要使用它获得主对角线另外一侧和当前 | ||
//正在被设置的项所对应项的模型索引,参数顺序是行号在列好之前,所以我们得颠倒一下顺序:column,row | ||
QModelIndex transposedIndex = createIndex(index.column(), | ||
index.row()); | ||
//修改整个区域(index,index)来刷新界面 | ||
emit dataChanged(index, index); | ||
emit dataChanged(transposedIndex, transposedIndex); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
QVariant CityModel::headerData(int section, | ||
Qt::Orientation /* orientation */, | ||
int role) const | ||
{ | ||
if (role == Qt::DisplayRole) | ||
return cities[section]; | ||
return QVariant(); | ||
} | ||
|
||
Qt::ItemFlags CityModel::flags(const QModelIndex &index) const | ||
{ | ||
//只有不是同一个城市,才能编辑其距离 | ||
Qt::ItemFlags flags = QAbstractItemModel::flags(index); | ||
if (index.row() != index.column()) | ||
flags |= Qt::ItemIsEditable; | ||
return flags; | ||
} | ||
|
||
int CityModel::offsetOf(int row, int column) const | ||
{ | ||
if (row < column) | ||
qSwap(row, column); | ||
//二维数组的索引下标 | ||
return (row * (row - 1) / 2) + column; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#ifndef CITYMODEL_H | ||
#define CITYMODEL_H | ||
|
||
#include <QAbstractTableModel> | ||
#include <QStringList> | ||
#include <QVector> | ||
|
||
namespace Ui { | ||
class CityModel; | ||
} | ||
|
||
class CityModel : public QAbstractTableModel | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
CityModel(QObject *parent = 0); | ||
|
||
void setCities(const QStringList &cityNames); | ||
int rowCount(const QModelIndex &parent) const; | ||
int columnCount(const QModelIndex &parent) const; | ||
QVariant data(const QModelIndex &index, int role) const; | ||
bool setData(const QModelIndex &index, const QVariant &value, | ||
int role); | ||
QVariant headerData(int section, Qt::Orientation orientation, | ||
int role) const; | ||
Qt::ItemFlags flags(const QModelIndex &index) const; | ||
|
||
private: | ||
int offsetOf(int row, int column) const; | ||
|
||
//保存城市名 | ||
QStringList cities; | ||
//保存不同城市之间的距离 | ||
QVector<int> distances; | ||
|
||
private: | ||
Ui::CityModel *ui; | ||
}; | ||
|
||
#endif // CITYMODEL_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<ui version="4.0"> | ||
<class>CityModel</class> | ||
<widget class="QDialog" name="CityModel" > | ||
<property name="geometry" > | ||
<rect> | ||
<x>0</x> | ||
<y>0</y> | ||
<width>400</width> | ||
<height>300</height> | ||
</rect> | ||
</property> | ||
<property name="windowTitle" > | ||
<string>CityModel</string> | ||
</property> | ||
</widget> | ||
<layoutDefault spacing="6" margin="11" /> | ||
<pixmapfunction></pixmapfunction> | ||
<resources/> | ||
<connections/> | ||
</ui> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include "citymodel.h" | ||
#include <QApplication> | ||
#include <QTableView> | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
QApplication a(argc, argv); | ||
QStringList cities; | ||
cities << "Arvika" << "Boden" << "Eskilstuna" << "Falun" | ||
<< "Filipstad" << "Halmstad" << "Helsingborg" << "Karlstad" | ||
<< "Kiruna" << "Kramfors" << "Motala" << "Sandviken" | ||
<< "Skara" << "Stockholm" << "Sundsvall" << "Trelleborg"; | ||
|
||
CityModel cityModel; | ||
cityModel.setCities(cities); | ||
|
||
QTableView tableView; | ||
tableView.setModel(&cityModel); | ||
tableView.setAlternatingRowColors(true); | ||
tableView.setWindowTitle(QObject::tr("Cities")); | ||
tableView.show(); | ||
|
||
return a.exec(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include <QtWidgets> | ||
|
||
#include "colorlisteditor.h" | ||
|
||
ColorListEditor::ColorListEditor(QWidget *widget) : QComboBox(widget) | ||
{ | ||
populateList(); | ||
} | ||
|
||
//返回所选择的数据---选择的数据存储在Qt::DecorationRole | ||
QColor ColorListEditor::color() const | ||
{ | ||
return qvariant_cast<QColor>(itemData(currentIndex(), Qt::DecorationRole)); | ||
} | ||
|
||
//从Qt::DecorationRole中将选择到的颜色属性设置为当前的选项 | ||
void ColorListEditor::setColor(QColor color) | ||
{ | ||
setCurrentIndex(findData(color, int(Qt::DecorationRole))); | ||
} | ||
|
||
//得到所有颜色属性并插入到列表框中 | ||
void ColorListEditor::populateList() | ||
{ | ||
QStringList colorNames = QColor::colorNames(); | ||
|
||
for (int i = 0; i < colorNames.size(); ++i) { | ||
QColor color(colorNames[i]); | ||
|
||
insertItem(i, colorNames[i]); | ||
setItemData(i, color, Qt::DecorationRole); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#ifndef COLORLISTEDITOR_H | ||
#define COLORLISTEDITOR_H | ||
|
||
#include <QComboBox> | ||
|
||
class QColor; | ||
class QWidget; | ||
|
||
class ColorListEditor : public QComboBox | ||
{ | ||
Q_OBJECT | ||
//自定义QColor属性 | ||
Q_PROPERTY(QColor color READ color WRITE setColor USER true) | ||
|
||
public: | ||
ColorListEditor(QWidget *widget = 0); | ||
|
||
public: | ||
QColor color() const; | ||
void setColor(QColor c); | ||
|
||
private: | ||
void populateList(); | ||
}; | ||
|
||
|
||
#endif // COLORLISTEDITOR_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#------------------------------------------------- | ||
# | ||
# Project created by QtCreator 2014-11-23T14:06:15 | ||
# | ||
#------------------------------------------------- | ||
|
||
QT += core gui | ||
|
||
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets | ||
|
||
TARGET = ColorListEditor | ||
TEMPLATE = app | ||
|
||
|
||
SOURCES += main.cpp\ | ||
window.cpp \ | ||
ColorListEditor.cpp | ||
|
||
HEADERS += window.h \ | ||
ColorListEditor.h | ||
|
||
FORMS += window.ui |
Oops, something went wrong.