本文整理汇总了C++中QSqlRelationalTableModel::setSort方法的典型用法代码示例。如果您正苦于以下问题:C++ QSqlRelationalTableModel::setSort方法的具体用法?C++ QSqlRelationalTableModel::setSort怎么用?C++ QSqlRelationalTableModel::setSort使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QSqlRelationalTableModel
的用法示例。
在下文中一共展示了QSqlRelationalTableModel::setSort方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: QWidget
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
resize(400,300);
if(connect())
{
QTableView *view = new QTableView(this);
QSqlRelationalTableModel *model = new QSqlRelationalTableModel;//要先有数据才能创建model,也就是先连接数据库局
view->setGeometry(0,0,400,300);
view->setModel(model);
model->setTable("student");
model->setSort(0,Qt::AscendingOrder);
// model->setHeaderData(0,Qt::Horizontal,"Name");//from 0
// model->setHeaderData(1,Qt::Horizontal,"Age");
// model->setHeaderData(2,Qt::Horizontal,"likes");
model->setRelation(3,QSqlRelation("city","id","name"));
model->select();
// view->setModel(model);
view->setSelectionMode(QAbstractItemView::SingleSelection);//设置选择模式
view->setSelectionBehavior(QAbstractItemView::SelectRows);//选择行
// view->resizeColumnsToContents();//tiaozhengziti
view->setEditTriggers(QAbstractItemView::DoubleClicked);//不可编辑?
QHeaderView *header = view->horizontalHeader();
header->setStretchLastSection(true);
view->setItemDelegate(new QSqlRelationalDelegate(view));
// QHeaderView *head = view->verticalHeader();
// head->setStretchLastSection(true);
// view->show();
// this?->show();
}
}
示例2: QMainWindow
/*
** public:
*/
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, m_timer(new QBasicTimer)
, m_dataProvider(new DataProvider(this))
, m_dialogSettings(0) // Guarded by QPointer
, m_dialogAbout(0) // Guarded by QPointer
{
qDebug() << Q_FUNC_INFO;
/*
m_dataModel = new QStandardItemModel(this);
m_dataModel->setColumnCount(5);
*/
setupUi(this);
/*
QStringList labels;
labels << tr("Show name") << tr("Episode name")
<< tr("Season #") << tr("Episode #") << tr("Date");
m_dataModel->setHorizontalHeaderLabels(labels);
// showsTableView->setModel(m_dataModel);
showsTableView->setModel(DbInterface::instance().readEpisodes());
showsTableView->verticalHeader()->hide();
showsTableView->horizontalHeader()->setResizeMode(QHeaderView::Interactive);
showsTableView->horizontalHeader()->setStretchLastSection(true);
showsTableView->horizontalHeader()->setCascadingSectionResizes(true);
showsTableView->horizontalHeader()->setHighlightSections(true);
showsTableView->horizontalHeader()->setMovable(true);
srand(QTime::currentTime().toString("hhmmsszzz").toUInt());
// Fill with random stuff
for (int i=0; i<30; ++i) {
m_dataModel->setItem(i, 0, new QStandardItem(QString("show_%1").arg(i)));
m_dataModel->setItem(i, 1, new QStandardItem(QString("episode_%1").arg(i)));
m_dataModel->setItem(i, 2, new QStandardItem(QString("%1").arg(rand()%10+1)));
m_dataModel->setItem(i, 3, new QStandardItem(QString("%1").arg(rand()%24+1)));
QDate rndDate = QDate::currentDate().addDays(i);
m_dataModel->setItem(i, 4, new QStandardItem(rndDate.toString("yyyy/MM/dd")));
}
*/
setWindowTitle(QString("nextShows - v%1").arg(NEXTSHOWS_VERSION));
statusBar()->showMessage(tr("nextShows started"), 1000*3);
// TEMPORARY CODE FOR TESTING ////////////////////////////////////////////
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "NS_View");
db.setDatabaseName("ns.db");
db.open();
QSqlRelationalTableModel *model = new QSqlRelationalTableModel(this, db);
model->setTable("T_Episodes");
model->setRelation(model->fieldIndex("shows_id"),
QSqlRelation("T_Shows", "idT_Shows", "ShowName"));
model->setSort(model->fieldIndex("Date"), Qt::AscendingOrder);
model->select();
QStringList hideFields;
hideFields << "idT_Episodes" << "ProdNumber" << "EpisodeCount"
<< "EpisodeUrl" << "isSpecial";
foreach (QString field, hideFields) {
model->removeColumn(model->fieldIndex(field));
}