0

如何绘制像http://jsbin.com/uzOtiw/1这样的表格?我对此一无所知。

易于理解的示例代码将很有帮助。

4

2 回答 2

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();
}
于 2013-09-11T20:38:50.703 回答
0

如果没有更多的上下文或更具体的解释你想要达到的目标,很难给出一个真正彻底的答案,但我最初的建议可能是尝试使用 Qt 的富文本支持。此页面提供了有关此的更多信息。特别注意它<table>是受支持的,因此您可以使用这些控件绘制任意 HTML 表格。

除此之外,我假设您已经看过QTableView

于 2013-09-11T20:29:27.717 回答