QTableView * MainWindow::createPropertyView() { QTableView *propertyView = new QTableView; propertyView->setFixedWidth(250); propertyView->setItemDelegateForRow(1, new ComboBoxDelegate(ComboBoxDelegate::ComboType::Precondition)); /* const int nHeaderCount = 2; QStandardItemModel *model = new QStandardItemModel; model->setColumnCount(nHeaderCount); model->setHeaderData(0, Qt::Horizontal, tr("Property")); model->setHeaderData(1, Qt::Horizontal, tr("Value")); propertyView->setModel(model); propertyView->horizontalHeader()->setDefaultAlignment(Qt::AlignLeft); propertyView->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Fixed); propertyView->setColumnWidth(0, 101); for (int i=0; i<nHeaderCount; ++i) { model->setItem(i, 0, new QStandardItem("2009441676")); model->item(i, 0)->setForeground(QBrush(QColor(255, 0, 0))); model->item(i, 0)->setTextAlignment(Qt::AlignCenter); model->setItem(i, 1, new QStandardItem(QString::fromLocal8Bit("¹þ¹þ"))); } */ return propertyView; }
/* we create a table that has a fixed height, but can stretch to fit certain width */ QTableView *PrintLayout::createProfileTable(ProfilePrintModel *model, const int tableW, const qreal fitNotesToHeight) { // 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, 3); table->setSpan(1, 0, 1, 3); table->setSpan(0, 3, 1, 2); table->setSpan(1, 3, 1, 2); // 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. * what fitNotesToHeight does it to expand the notes section to fit a special height */ int i; profilePrintTableMaxH = 0; for (i = 0; i < rows; i++) { int h = (i == rows - 1 && fitNotesToHeight != 0.0) ? fitNotesToHeight : 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(table)); table->setItemDelegateForRow(7, new HTMLDelegate(table)); table->setShowGrid(false); table->setStyleSheet( "QTableView { border: none }" "QTableView::item { border: 0px; padding-left: 2px; padding-right: 2px; }"); // return return table; }