编辑:抱歉,我没有看到您想要 Qt 中的 可打印 表。我在QTableWidget 元素中提供了一种方法。
此代码未经编译且未经测试,但我相信它可以适当地重现您想要的表格:
const int ROW = 6;
const int COL = 8;
int main(int argc, char **argv) {
QApplication app(argc, argv);
QMainWindow *window = new QMainWindow();
window->resize(400, 250);
QTableWidget* table = new QTableWidget();
//Set table row count 1 and column count 3
table->setRowCount(ROW);
table->setColumnCount(COL);
table->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
// generate the table widgets (first column)
QTableWidgetItem* sl = new QTableWidgetItem("SL");
QTableWidgetItem* one = new QTableWidgetItem("1");
QTableWidgetItem* two = new QTableWidgetItem("2");
QTableWidgetItem* three = new QTableWidgetItem("3");
QTableWidgetItem* total = new QTableWidgetItem("Total");
//Add Table items here
table->setItem(0, 0, sl);
table->setSpan(0, 0, 2, 1); // set the sl span
table->setItem(0, 2, one);
table->setItem(0, 3, two);
table->setItem(0, 4, three);
table->setItem(0, 5, total);
// generate the table widgets for the second and third columns
QTableWidgetItem* unit1 = new QTableWidgetItem("Unit1");
unit1->setAlignment(Qt::AlignCenter);
QTableWidgetItem *unit2 = new QTableWidgetItem("Unit2");
unit2->setAlignment(Qt::AlignCenter);
QTableWidgetItem *unit3 = new QTableWidgetItem("Unit2");
unit3->setAlignment(Qt::AlignCenter);
QTableWidgetItem *comments = new QTableWidgetItem("Comments");
// set the unit things and comments
table->setItem(0, 1, unit1);
table->setSpan(0, 1, 1, 2);
table->setItem(0, 3, unit2)
table->setSpan(0, 3, 1, 2);
table->setItem(0, 5, unit3);
table->setSpan(0, 5, 1, 2);
table->setItem(0, 7, comments);
// now set up the product widgets
QTableWidgetItem *product = new QTableWidgetItem("Product");
QTableWidgetItem *product2 = new QTableWidgetItem("Product2");
table->setItem(1, 1, product);
table->setItem(1, 2, product2);
table->setItem(1, 3, product);
table->setItem(1, 4, product2);
table->setItem(1, 5, product);
table->setItem(1, 6, product2);
window->setCentralWidget(table);
window->show();
return app.exec();
}