答案 0 :(得分:1)
编辑:抱歉,我没有在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();
}
答案 1 :(得分:0)
如果没有更多的背景或更准确地解释你想要实现的目标,很难给出一个非常彻底的答案,但我最初的建议可能是尝试使用Qt的富文本支持。 This page提供了有关此内容的更多信息。特别要注意,<table>
受支持,因此您可以使用这些控件绘制任意HTML表格。
除此之外,我假设你已经看过QTableView了?