int main(int argc, char* argv[]) { QApplication app(argc, argv); QFrame frame; QVBoxLayout verticalLayout; QLineEdit filterText; ProblemsTableModel submissions; // add data here QSortFilterProxyModel proxy; proxy.setSortCaseSensitivity(Qt::CaseInsensitive); proxy.setFilterCaseSensitivity(Qt::CaseInsensitive); proxy.setSourceModel(&submissions); // sort and filter by "Full name" column proxy.setFilterKeyColumn(2); QTableView tableView; tableView.setModel(&proxy); tableView.setSortingEnabled(true); verticalLayout.addWidget(&filterText); verticalLayout.addWidget(&tableView); frame.setLayout(&verticalLayout); QObject::connect(&filterText, &QLineEdit::textChanged, &proxy, &QSortFilterProxyModel::setFilterFixedString); frame.show(); return app.exec(); }
void AddressWidget::setupTabs() { QStringList groups; groups << "ABC" << "DEF" << "GHI" << "JKL" << "MNO" << "PQR" << "STU" << "VW" << "XYZ"; for (int i = 0; i < groups.size(); ++i) { QString str = groups.at(i); proxyModel = new QSortFilterProxyModel(this); proxyModel->setSourceModel(table); proxyModel->setDynamicSortFilter(true); QTableView *tableView = new QTableView; tableView->setModel(proxyModel); tableView->setSortingEnabled(true); tableView->setSelectionBehavior(QAbstractItemView::SelectRows); tableView->horizontalHeader()->setStretchLastSection(true); tableView->verticalHeader()->hide(); tableView->setEditTriggers(QAbstractItemView::NoEditTriggers); tableView->setSelectionMode(QAbstractItemView::SingleSelection); QString newStr = QString("^[%1].*").arg(str); proxyModel->setFilterRegExp(QRegExp(newStr, Qt::CaseInsensitive)); proxyModel->setFilterKeyColumn(0); proxyModel->sort(0, Qt::AscendingOrder); connect(tableView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SIGNAL(selectionChanged(QItemSelection))); addTab(tableView, str); } }
// Table factory QTableView* MakeTableView(QAbstractItemModel* model, bool sortingEnabled = true, uint32_t modelSortColumn = 0) { class MyTable : public QTableView { public: QStyleOptionViewItem viewOptions() const override { QStyleOptionViewItem option = QTableView::viewOptions(); option.decorationAlignment = Qt::AlignHCenter | Qt::AlignCenter; option.decorationPosition = QStyleOptionViewItem::Top; return option; } void mouseMoveEvent(QMouseEvent* event) override { QModelIndex index = indexAt(event->pos()); if (index.isValid()) { QVariant data = model()->data(index, PlayerTableModel::CursorRole); Qt::CursorShape shape = Qt::ArrowCursor; if (!data.isNull()) { shape = Qt::CursorShape(data.toInt()); } setCursor(shape); } QTableView::mouseMoveEvent(event); } }; QTableView* tableView = new MyTable(); tableView->setModel(model); tableView->setSortingEnabled(sortingEnabled); if (sortingEnabled) { tableView->sortByColumn(FindColumn(model, modelSortColumn)); } tableView->verticalHeader()->hide(); tableView->setAlternatingRowColors(true); tableView->verticalHeader()->setDefaultSectionSize(15); tableView->resizeColumnsToContents(); tableView->horizontalHeader()->setStretchLastSection(true); tableView->setSelectionBehavior(QAbstractItemView::SelectRows); tableView->setSelectionMode(QAbstractItemView::SingleSelection); tableView->setFocusPolicy(Qt::StrongFocus); // enable mouse tracking tableView->setMouseTracking(true); tableView->viewport()->setMouseTracking(true); tableView->installEventFilter(this); tableView->viewport()->installEventFilter(this); return tableView; }
void LoungeWidget::setupUI() { QVBoxLayout *mainLayout = new QVBoxLayout(); QTableView *connected = new QTableView(); mainLayout->addWidget(connected); connected->setModel(m_connectedPlayerList); connected->setSortingEnabled(true); connected->resizeColumnsToContents(); connected->horizontalHeader()->setStretchLastSection(true); connected->sortByColumn(0, Qt::AscendingOrder); connect(connected, SIGNAL(activated(QModelIndex)), this, SLOT(processActivatedRow(QModelIndex))); setLayout(mainLayout); }
int main(int argc, char *argv[]) { QApplication a(argc, argv); //QListView view; QTableView view; view.setSortingEnabled(false); view.horizontalHeader()->setStretchLastSection(true); view.horizontalHeader()->setVisible(false); view.verticalHeader()->setVisible(false); MillionRows model(Q_NULLPTR); view.setModel(&model); view.setAlternatingRowColors(true); view.setStyleSheet("alternate-background-color: green;background-color: white;"); view.setItemDelegateForColumn(0, new TableStyleDelegate()); view.resizeRowsToContents(); // impact performance - force call sizeHint view.show(); return a.exec(); }
QmitkModulesDialog::QmitkModulesDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f) { this->setWindowTitle("MITK Modules"); QVBoxLayout* layout = new QVBoxLayout(); this->setLayout(layout); QTableView* tableView = new QTableView(this); QmitkModuleTableModel* tableModel = new QmitkModuleTableModel(tableView); QSortFilterProxyModel* sortProxyModel = new QSortFilterProxyModel(tableView); sortProxyModel->setSourceModel(tableModel); sortProxyModel->setDynamicSortFilter(true); tableView->setModel(sortProxyModel); tableView->verticalHeader()->hide(); tableView->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel); tableView->setSelectionBehavior(QAbstractItemView::SelectRows); tableView->setSelectionMode(QAbstractItemView::ExtendedSelection); tableView->setTextElideMode(Qt::ElideMiddle); tableView->setSortingEnabled(true); tableView->sortByColumn(0, Qt::AscendingOrder); tableView->horizontalHeader()->setResizeMode(0, QHeaderView::ResizeToContents); tableView->horizontalHeader()->setResizeMode(2, QHeaderView::ResizeToContents); tableView->horizontalHeader()->setResizeMode(5, QHeaderView::ResizeToContents); tableView->horizontalHeader()->setStretchLastSection(true); tableView->horizontalHeader()->setCascadingSectionResizes(true); layout->addWidget(tableView); QDialogButtonBox* btnBox = new QDialogButtonBox(QDialogButtonBox::Close); layout->addWidget(btnBox); this->resize(800, 600); connect(btnBox, SIGNAL(rejected()), this, SLOT(reject())); }
QSortFilterProxyModel * GuiTools::ModelToViewWithFilter(QAbstractItemModel *model, QAbstractItemView *view,QLineEdit* lineEdit,QSortFilterProxyModel *proxyModel) { view->reset(); if(!proxyModel) proxyModel = new QSortFilterProxyModel((QObject*)lineEdit); proxyModel->setSourceModel(model); view->setModel(proxyModel); //view->horizontalHeader()->setResizeMode(QHeaderView::Stretch); QTableView* tableView = dynamic_cast<QTableView*>(view); if(tableView) { tableView->horizontalHeader()->setStretchLastSection(true); tableView->resizeColumnsToContents(); tableView->resizeRowsToContents(); tableView->setSortingEnabled(true); tableView->verticalHeader()->hide(); } QTreeView* treeView = dynamic_cast<QTreeView*>(view); if(treeView) { for(int i=0;i<model->columnCount();i++) treeView->resizeColumnToContents(i); treeView->setSortingEnabled(true); } // views' filter proxyModel->setFilterKeyColumn(0); proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive); connect(lineEdit,SIGNAL(textChanged(const QString&)), proxyModel,SLOT(setFilterWildcard(const QString&)),Qt::AutoConnection); return proxyModel; }
int main(int argc, char *argv[]) { QApplication app(argc, argv); QWidget * main_wg = new QWidget; // cria um objeto "splitter" para compartilhar widgets: QSplitter *splitter = new QSplitter(main_wg); // cria um "model" usando o "StandardModel" QStandardItemModel *model = new QStandardItemModel; const int totCols = 3; int col; // define os títulos das colunas: for (col = 0; col < totCols; ++col) { model->setHorizontalHeaderItem(col, new QStandardItem( QString("COL-%1").arg(col+1) ) ); } // alimenta linhas, colunas e sub-níveis: QStandardItem *parentItem = model->invisibleRootItem(); const int iniLevel = 0; const int totLevels= 3; QString prevRows(""); QVector<QSize> vec_ColsRows; // colunas, linhas de cada nível vec_ColsRows.reserve( totLevels ); // quantidade-colunas, quantidade-linhas vec_ColsRows << QSize(3,10) << QSize(3,3) << QSize(3,2) ; populate_model ( parentItem, vec_ColsRows, iniLevel, prevRows); // Neste exemplo, // O "model" foi alimentado com linhas, colunas e sub-níveis: // E serão criadas 4 "views" (uma "tree", uma "table", uma "list" e uma "comboBox") // relacionadas ao mesmo "model"; // Cada "view" exibe os dados de uma determinada maneira; // 1- ==== a primeira "view" é uma "tree": QTreeView *tree = new QTreeView(splitter); tree->setModel(model); // habilita classificação na tree: tree->setSortingEnabled(true); // classifica tree->sortByColumn(0); // expande toda a árvore: tree->expandAll(); // força largura de todas as colunas // para exibição completa do texto dos seus itens for (col = 0; col < totCols; ++col) tree->resizeColumnToContents(col); // configura o header para permitir mudança na ordem de classificacão: QHeaderView * hdrTree = tree->header(); hdrTree->setClickable (true); hdrTree->setSortIndicator(0,Qt::AscendingOrder); hdrTree->setSortIndicatorShown(true); hdrTree->setMovable(true); // permite mover colunas do header // 2- ==== a segunda "view" é uma "table" QTableView *table = new QTableView(splitter); table->setModel(model); table->setAlternatingRowColors(true); // habilita classificação na table: table->setSortingEnabled(true); // classifica table->sortByColumn(0); // configura o header para permitir mudança na ordem de classificacão: QHeaderView * hdrTable = table->horizontalHeader(); hdrTable->setClickable (true); hdrTable->setSortIndicator(0,Qt::AscendingOrder); hdrTable->setSortIndicatorShown(true); hdrTable->setMovable(true); // permite mover colunas do header // 3- ==== a terceira view é uma "list": QListView *list = new QListView(splitter); list->setModel(model); // 4- ==== a quarta "view" é uma "comboBox" QComboBox *combo = new QComboBox; combo->setModel(model); // configura a "splitter" definindo a largura de cada "view" int width = 800; QList< int > cols; cols << int(width* 0.45) << int(width*0.45) << int(width*0.1); splitter->setSizes(cols); // layout para agrupar a "combo" e a "splitter": QGridLayout * glayMain = new QGridLayout; main_wg->setLayout( glayMain); glayMain->addWidget( combo, 0, 1); // linha 0, coluna 0; glayMain->setRowMinimumHeight(1, glayMain->verticalSpacing() * 4); // linha 1: linha de separação glayMain->addWidget( splitter, 2, 0, 1, 3 ); // linha 2, coluna 0, rowSpan 1, colSpan 3 main_wg->setWindowTitle("06_standard - 4 'views' usando o mesmo 'model' (StandardModel) - recursivo"); main_wg->resize(800,500); main_wg->show(); return app.exec(); }