• QTableWidget的简单用法


      QTableWidget是一个显示结果类似一个表格,下面是它的简单使用。

      main.cpp文件如下:

    #include <QtGui/QApplication>
    #include <QTextCodec>
    #include <QTableWidget>
    #include <QWidget>
    #include <QVBoxLayout>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QTextCodec::setCodecForTr(QTextCodec::codecForName("gb18030"));
        QTableWidget *table = new QTableWidget;
        table->setRowCount(3);
        table->setColumnCount(3);
    
        QStringList horizontalHeader;
        horizontalHeader<<QObject::tr("年龄")<<QObject::tr("身高")<<QObject::tr("体重");
        QStringList verticalHeader;
        verticalHeader<<QObject::tr("张三")<<QObject::tr("李四")<<QObject::tr("王五");
    
        table->setHorizontalHeaderLabels(horizontalHeader);
        table->setVerticalHeaderLabels(verticalHeader);
    
        table->setItem(0, 0, new QTableWidgetItem(QString("23")));
        table->setItem(0, 1, new QTableWidgetItem(QString("177cm")));
        table->setItem(0, 2, new QTableWidgetItem(QString("60kg")));
        table->setItem(1, 0, new QTableWidgetItem(QString("25")));
        table->setItem(1, 1, new QTableWidgetItem(QString("180cm")));
        table->setItem(1, 2, new QTableWidgetItem(QString("70kg")));
        table->setItem(2, 0, new QTableWidgetItem(QString("29")));
        table->setItem(2, 1, new QTableWidgetItem(QString("172cm")));
        table->setItem(2, 2, new QTableWidgetItem(QString("65kg")));
    
        QWidget *widget = new QWidget;
        QVBoxLayout *mainlayout = new QVBoxLayout;
        mainlayout->addWidget(table);
    
        widget->setLayout(mainlayout);
        widget->resize(400, 200);
        widget->setWindowTitle(QObject::tr("QTableWidget Demo"));
        widget->show();
    
        return a.exec();
    }

      运行效果:

                                           

  • 相关阅读:
    Python-05知识-01Python优缺点
    Python-02进阶-06代码优化工具
    Python-02进阶-04多进程多线程
    Python-02进阶-03生成器
    Python-02进阶-02装饰器
    Python-01基础-13功能模块
    Python-01基础-12常用命令
    Python-01基础-11基础知识
    console.dir有很多浏览器,系统的兼容性问题,不要随便用!
    微信支付-签名错误
  • 原文地址:https://www.cnblogs.com/venow/p/2728277.html
Copyright © 2020-2023  润新知