//! [0] int main(int argc, char *argv[]) { QApplication app(argc, argv); QStandardItemModel model(4, 2); QTableView tableView; tableView.setModel(&model); SpinBoxDelegate delegate; tableView.setItemDelegate(&delegate); //! [0] tableView.horizontalHeader()->setStretchLastSection(true); //! [1] for (int row = 0; row < 4; ++row) { for (int column = 0; column < 2; ++column) { QModelIndex index = model.index(row, column, QModelIndex()); model.setData(index, QVariant((row+1) * (column+1))); } //! [1] //! [2] } //! [2] //! [3] tableView.setWindowTitle(QObject::tr("Spin Box Delegate")); tableView.show(); return app.exec(); }
/*! Create and return the table used to visualize digital signals. */ QTableView* UiDigitalGenerator::createTable() { // Deallocation: "Qt Object trees" (See UiMainWindow) QTableView* table = new QTableView(this); QItemSelectionModel* m = table->selectionModel(); if (m != NULL) delete m; table->setModel(mSignals); // Deallocation: "Qt Object trees" (See UiMainWindow) DigitalDelegate* delegate = new DigitalDelegate(this); QAbstractItemDelegate* d = table->itemDelegate(); if (d != NULL) delete d; table->setItemDelegate(delegate); table->resizeColumnsToContents(); table->resizeRowsToContents(); connect(table->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(handleSelectionChanged(QItemSelection,QItemSelection))); return table; }
QTableView* EkonTables::createRelationTableView(QWidget *parentWidget, QSqlRelationalTableModel *model) { QTableView *tableView = createTableView(parentWidget, model); tableView->setItemDelegate(new QSqlRelationalDelegate(tableView)); return tableView; }
ErrorListView::ErrorListView(QWidget* parent) : QWidget(parent), m_model(0) { m_model = new ErrorListModel(this); QTableView* tableView = new QTableView; tableView->setItemDelegate(new ItemDelegate); tableView->setSelectionBehavior(QAbstractItemView::SelectRows); tableView->setAlternatingRowColors(true); tableView->setShowGrid(false); tableView->verticalHeader()->hide(); tableView->setModel(m_model); #ifdef STROMX_STUDIO_QT4 tableView->horizontalHeader()->setResizeMode(ErrorListModel::TIME, QHeaderView::Interactive); tableView->horizontalHeader()->setResizeMode(ErrorListModel::DESCRIPTION, QHeaderView::Stretch); #else tableView->horizontalHeader()->setSectionResizeMode(ErrorListModel::TIME, QHeaderView::Interactive); tableView->horizontalHeader()->setSectionResizeMode(ErrorListModel::DESCRIPTION, QHeaderView::Stretch); #endif // STROMX_STUDIO_QT4 QPushButton* clearButton = new QPushButton(tr("Clear error log")); connect(clearButton, SIGNAL(clicked()), m_model, SLOT(clear())); QHBoxLayout* buttonLayout = new QHBoxLayout; buttonLayout->addWidget(clearButton); buttonLayout->addStretch(); QVBoxLayout* mainLayout = new QVBoxLayout; mainLayout->addWidget(tableView); mainLayout->addLayout(buttonLayout); setLayout(mainLayout); }
void tst_QItemDelegate::QTBUG4435_keepSelectionOnCheck() { QStandardItemModel model(3, 1); for (int i = 0; i < 3; ++i) { QStandardItem *item = new QStandardItem(QLatin1String("Item ") + QString::number(i)); item->setCheckable(true); item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled); model.setItem(i, item); } QTableView view; view.setModel(&model); view.setItemDelegate(new TestItemDelegate); view.show(); view.selectAll(); QVERIFY(QTest::qWaitForWindowExposed(&view)); QStyleOptionViewItem option; option.rect = view.visualRect(model.index(0, 0)); // mimic QStyledItemDelegate::initStyleOption logic option.features = QStyleOptionViewItem::HasDisplay | QStyleOptionViewItem::HasCheckIndicator; option.checkState = Qt::CheckState(model.index(0, 0).data(Qt::CheckStateRole).toInt()); const int checkMargin = qApp->style()->pixelMetric(QStyle::PM_FocusFrameHMargin, 0, 0) + 1; QPoint pos = qApp->style()->subElementRect(QStyle::SE_ViewItemCheckIndicator, &option, 0).center() + QPoint(checkMargin, 0); QTest::mouseClick(view.viewport(), Qt::LeftButton, Qt::ControlModifier, pos); QTRY_VERIFY(view.selectionModel()->isColumnSelected(0, QModelIndex())); QCOMPARE(model.item(0)->checkState(), Qt::Checked); }
QTableView *createView(const QString &title, QSqlTableModel *model) { QTableView *view = new QTableView; view->setModel(model); view->setItemDelegate(new QSqlRelationalDelegate(view)); view->setWindowTitle(title); return view; }
int main(int argc, char *argv[]) { QApplication app(argc, argv); QTableView* table = new QTableView(); TableModel* model = new TableModel(table); MultipleSelectDelegate* delegate = new MultipleSelectDelegate(table); table->setModel(model); table->setItemDelegate(delegate); table->show(); return app.exec(); }
int main(int argc, char *argv[]) { QApplication app(argc, argv); QTableView tv; QStandardItemModel m; m.setRowCount(4); m.setColumnCount(2); tv.setModel(&m); tv.show(); tv.setItemDelegate(new ExampleDelegate()); app.exec(); }
Departments::Departments(QSqlDatabase& db, QWidget *parent): QDialog(parent), ui(new Ui::Departments), _db(&db) { ui->setupUi(this); _searchAgain = true; _model = new QSqlRelationalTableModel(this, *_db); _model->setTable("departments"); _model->setEditStrategy(QSqlTableModel::OnFieldChange); _model->setRelation(2, QSqlRelation("faculties", "fc_id", "fc_name")); reloadTable(); QTableView* view = ui->tableView; view->setModel(_model); view->setItemDelegate(new QSqlRelationalDelegate(view)); }
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(); } }
MainWindow::MainWindow(QWidget* parent, Qt::WindowFlags f) : QMainWindow(parent, f), configurationModels(), geometryModels(), ikAlgorithmComboBox(new QComboBox(this)), ikDurationSpinBox(new QSpinBox(this)), ikIterationsSpinBox(new QSpinBox(this)), ikJacobianComboBox(new QComboBox(this)), kinematicModels(), operationalModels(), scene(), configurationDelegates(), configurationDockWidget(new QDockWidget(this)), configurationTabWidget(new QTabWidget(this)), configurationViews(), gradientBackground(), operationalDelegates(), operationalDockWidget(new QDockWidget(this)), operationalTabWidget(new QTabWidget(this)), operationalViews(), saveImageWithAlphaAction(new QAction(this)), saveImageWithoutAlphaAction(new QAction(this)), saveSceneAction(new QAction(this)), server(new Server(this)), viewer(nullptr) { MainWindow::singleton = this; SoQt::init(this); SoDB::init(); SoGradientBackground::initClass(); std::shared_ptr<rl::sg::Factory> geometryFactory; std::string geometryFilename = QApplication::arguments()[1].toStdString(); if ("urdf" == geometryFilename.substr(geometryFilename.length() - 4, 4)) { geometryFactory = std::make_shared<rl::sg::UrdfFactory>(); } else { geometryFactory = std::make_shared<rl::sg::XmlFactory>(); } this->scene = std::make_shared<rl::sg::so::Scene>(); geometryFactory->load(geometryFilename, this->scene.get()); for (int i = 2; i < QApplication::arguments().size(); ++i) { std::shared_ptr<rl::mdl::Factory> kinematicFactory; std::string kinematicFilename = QApplication::arguments()[i].toStdString(); if ("urdf" == kinematicFilename.substr(kinematicFilename.length() - 4, 4)) { kinematicFactory = std::make_shared<rl::mdl::UrdfFactory>(); } else { kinematicFactory = std::make_shared<rl::mdl::XmlFactory>(); } this->geometryModels.push_back(this->scene->getModel(i - 2)); this->kinematicModels.push_back(std::dynamic_pointer_cast<rl::mdl::Kinematic>(kinematicFactory->create(kinematicFilename))); } for (std::size_t i = 0; i < this->kinematicModels.size(); ++i) { ConfigurationDelegate* configurationDelegate = new ConfigurationDelegate(this); configurationDelegate->id = i; this->configurationDelegates.push_back(configurationDelegate); ConfigurationModel* configurationModel = new ConfigurationModel(this); configurationModel->id = i; this->configurationModels.push_back(configurationModel); QTableView* configurationView = new QTableView(this); #if QT_VERSION >= 0x050000 configurationView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); #else // QT_VERSION configurationView->horizontalHeader()->setResizeMode(QHeaderView::Stretch); #endif // QT_VERSION configurationView->horizontalHeader()->hide(); configurationView->setAlternatingRowColors(true); configurationView->setItemDelegate(configurationDelegate); configurationView->setModel(configurationModel); this->configurationViews.push_back(configurationView); this->configurationTabWidget->addTab(configurationView, QString::number(i)); OperationalDelegate* operationalDelegate = new OperationalDelegate(this); this->operationalDelegates.push_back(operationalDelegate); OperationalModel* operationalModel = new OperationalModel(this); operationalModel->id = i; this->operationalModels.push_back(operationalModel); QTableView* operationalView = new QTableView(this); #if QT_VERSION >= 0x050000 operationalView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); #else // QT_VERSION operationalView->horizontalHeader()->setResizeMode(QHeaderView::Stretch); #endif // QT_VERSION operationalView->setAlternatingRowColors(true); operationalView->setItemDelegate(operationalDelegate); operationalView->setModel(operationalModel); this->operationalViews.push_back(operationalView); this->operationalTabWidget->addTab(operationalView, QString::number(i)); QObject::connect( configurationModel, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), operationalModel, SLOT(configurationChanged(const QModelIndex&, const QModelIndex&)) ); QObject::connect( operationalModel, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), configurationModel, SLOT(operationalChanged(const QModelIndex&, const QModelIndex&)) ); configurationModel->setData(this->kinematicModels[i]->getHomePosition()); } this->configurationDockWidget->resize(160, 320); this->configurationDockWidget->setWidget(this->configurationTabWidget); this->configurationDockWidget->setWindowTitle("Configuration"); this->operationalDockWidget->resize(160, 320); this->operationalDockWidget->setWidget(this->operationalTabWidget); this->operationalDockWidget->setWindowTitle("Operational"); this->addDockWidget(Qt::LeftDockWidgetArea, this->configurationDockWidget); this->addDockWidget(Qt::BottomDockWidgetArea, this->operationalDockWidget); #if QT_VERSION >= 0x050600 QList<QDockWidget*> resizeDocksWidgets; resizeDocksWidgets.append(this->operationalDockWidget); QList<int> resizeDocksSizes; resizeDocksSizes.append(1); this->resizeDocks(resizeDocksWidgets, resizeDocksSizes, Qt::Vertical); #endif // QT_VERSION this->ikAlgorithmComboBox->addItem("JacobianInverseKinematics"); #ifdef RL_MDL_NLOPT this->ikAlgorithmComboBox->addItem("NloptInverseKinematics"); #endif this->ikAlgorithmComboBox->setToolTip("IK Algorithm"); #ifdef RL_MDL_NLOPT this->ikAlgorithmComboBox->setCurrentIndex(this->ikAlgorithmComboBox->findText("NloptInverseKinematics")); #endif QObject::connect(this->ikAlgorithmComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(changeIkAlgorithm())); this->ikDurationSpinBox->setMaximum(10000); this->ikDurationSpinBox->setMinimum(1); this->ikDurationSpinBox->setSuffix(" ms"); this->ikDurationSpinBox->setToolTip("Max. IK Duration"); this->ikDurationSpinBox->setValue(500); this->ikIterationsSpinBox->setMaximum(100000); this->ikIterationsSpinBox->setMinimum(1); this->ikIterationsSpinBox->setToolTip("Max. IK Iterations"); this->ikIterationsSpinBox->setValue(10000); this->ikJacobianComboBox->addItem("DLS"); this->ikJacobianComboBox->addItem("SVD"); this->ikJacobianComboBox->addItem("Transpose"); this->ikJacobianComboBox->setToolTip("Jacobian Method"); this->ikJacobianComboBox->setCurrentIndex(this->ikJacobianComboBox->findText("SVD")); this->statusBar()->addPermanentWidget(this->ikAlgorithmComboBox); this->statusBar()->addPermanentWidget(this->ikJacobianComboBox); this->statusBar()->addPermanentWidget(this->ikIterationsSpinBox); this->statusBar()->addPermanentWidget(this->ikDurationSpinBox); this->changeIkAlgorithm(); this->gradientBackground = new SoGradientBackground(); this->gradientBackground->ref(); this->gradientBackground->color0.setValue(0.8f, 0.8f, 0.8f); this->gradientBackground->color1.setValue(1.0f, 1.0f, 1.0f); this->scene->root->insertChild(this->gradientBackground, 0); this->viewer = new SoQtExaminerViewer(this, nullptr, true, SoQtFullViewer::BUILD_POPUP); this->viewer->setSceneGraph(this->scene->root); this->viewer->setTransparencyType(SoGLRenderAction::SORTED_OBJECT_BLEND); this->viewer->viewAll(); this->resize(1024, 768); this->setCentralWidget(this->viewer->getWidget()); this->setWindowIconText("rlCoachMdl"); this->setWindowTitle("rlCoachMdl"); this->init(); this->server->listen(QHostAddress::Any, 11235); }
/* we create a table that has a fixed height, but can stretch to fit certain width */ QTableView *PrintLayout::createProfileTable(ProfilePrintModel *model, const int tableW) { // setup a new table QTableView *table = new QTableView(); QHeaderView *vHeader = table->verticalHeader(); QHeaderView *hHeader = table->horizontalHeader(); table->setAttribute(Qt::WA_DontShowOnScreen); table->setSelectionMode(QAbstractItemView::NoSelection); table->setFocusPolicy(Qt::NoFocus); table->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); table->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); hHeader->setVisible(false); vHeader->setVisible(false); #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0) hHeader->setResizeMode(QHeaderView::Fixed); vHeader->setResizeMode(QHeaderView::Fixed); #else hHeader->setSectionResizeMode(QHeaderView::Fixed); vHeader->setSectionResizeMode(QHeaderView::Fixed); #endif // set the model table->setModel(model); /* setup cell span for the table using QTableView::setSpan(). * changes made here reflect on ProfilePrintModel::data(). */ const int cols = model->columnCount(); const int rows = model->rowCount(); // info on top table->setSpan(0, 0, 1, 4); table->setSpan(1, 0, 1, 4); // gas used table->setSpan(2, 0, 1, 2); table->setSpan(3, 0, 1, 2); // notes table->setSpan(6, 0, 1, 5); table->setSpan(7, 0, 5, 5); /* resize row heights to the 'profilePrintRowHeights' indexes. * profilePrintTableMaxH will then hold the table height. */ int i; profilePrintTableMaxH = 0; for (i = 0; i < rows; i++) { int h = profilePrintRowHeights.at(i); profilePrintTableMaxH += h; vHeader->resizeSection(i, h); } // resize columns. columns widths are percentages from the table width. int accW = 0; for (i = 0; i < cols; i++) { int pw = qCeil((qreal)(profilePrintColumnWidths.at(i) * tableW) / 100.0); accW += pw; if (i == cols - 1 && accW > tableW) /* adjust last column */ pw -= accW - tableW; hHeader->resizeSection(i, pw); } // resize table->resize(tableW, profilePrintTableMaxH); // hide the grid and set a stylesheet table->setItemDelegate(new ProfilePrintDelegate(this)); table->setShowGrid(false); table->setStyleSheet( "QTableView { border: none }" "QTableView::item { border: 0px; padding-left: 2px; padding-right: 2px; }"); // return return table; }