【问题标题】:Drawing a complex printable table in qt在 qt 中绘制复杂的可打印表格
【发布时间】:2013-09-11 20:00:13
【问题描述】:

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

易于理解的示例代码会有所帮助。

【问题讨论】:

  • 我相信 Qxt 库能够像您在示例中那样创建标题。至于打印,我通过生成 html 来做到这一点。我相信我前段时间在这里回答了一个类似的问题,所以我会寻找那个..
  • 嗯。我可能对 Qxt 有误解。我似乎无法在 QxtHeaderView libqxt.bitbucket.org/doc/0.6/classes.html 中找到该功能
  • 我看到人们在谈论印刷,但问题中哪里提到了印刷?他只是问“我怎么画桌子”——画它的目的是什么,没有说明。无论如何,如果你想打印它,那么我认为富文本/HTML 是你最好的选择,或者如果它只用于应用程序本身,那么 QTableView。
  • 当前标题为“在 qt 中绘制复杂的可打印表格”。对我来说,可打印意味着用户希望能够将表格打印到打印机。

标签: qt qtableview


【解决方案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();
}

【讨论】:

  • 感谢您提供有价值的示例代码。它像我想看到的那样工作。再次感谢!但是,这次我想从 mysql 数据库中加载所有数据。我想知道我是否可以将相同的技术应用于 QSqlQueryModel 。 [对不起,我是新人]
  • 不知道我是怎么做到的!
  • 帮我在QsqlQueryModel中做?我想我需要插入第一行,但它不会被自动覆盖吗?尝试将此代码考虑到下一个级别。
  • 为此提出一个新问题。
【解决方案2】:

如果没有更多上下文或更具体的解释来说明您想要实现的目标,很难给出真正彻底的答案,但我最初的建议可能是尝试使用 Qt 的富文本支持。 This page 提供了更多相关信息。特别注意<table>是支持的,所以你可以使用这些控件绘制任意的HTML表格。

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

【讨论】:

    猜你喜欢
    • 2022-08-23
    • 2011-03-02
    • 1970-01-01
    • 1970-01-01
    • 2013-04-07
    • 1970-01-01
    • 2019-08-13
    • 1970-01-01
    • 2011-11-13
    相关资源
    最近更新 更多