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(); } }
/* ** 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)); }