如何将QString或QStringList传递给QTableWidgetItem

时间:2015-03-30 20:38:27

标签: c++ arrays qt

我正在尝试使用QTableViewQTableWidgetItem创建表格。

基本上我正在尝试在第一个Header下创建一些带有某个名称的行。

但问题是我正在通过一些名字传递每一行。

我希望使用QStringQStringList数组实现同样的目标。

目前我没有在第二个标题下传递任何值。

请建议如何做到这一点。请找到输出窗口。

以下是我的代码段:

#include <QApplication>
#include <QtGui>
#include <QMainWindow>
#include <QTableWidget>
#include <QString>

#define ROW_NUMBER 20
#define COL_NUMBER 2

int main(int argc , char **argv)
{
    QApplication app(argc,argv); 

    QMainWindow *window = new QMainWindow();

    window->setWindowTitle(QString::fromUtf8("TableWidget Resize column width"));
    window->resize(200,250);

    QTableWidget *table = new QTableWidget();
    table->setRowCount(ROW_NUMBER);
    table->setColumnCount(COL_NUMBER);

    table->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);

    table->setHorizontalHeaderLabels(QString("Field;Value").split(";"));

    table->setItem(0,0,new QTableWidgetItem("Sequence Number"));
    table->setItem(1,0,new QTableWidgetItem("Date"));
    table->setItem(2,0,new QTableWidgetItem("Seconds"));
    table->setItem(3,0,new QTableWidgetItem("Source Date"));
    table->setItem(4,0,new QTableWidgetItem("Source Time"));
    table->setItem(5,0,new QTableWidgetItem("ServiceType"));
    table->setItem(6,0,new QTableWidgetItem("Transtype"));
    table->setItem(7,0,new QTableWidgetItem("UserId"));
    table->setItem(8,0,new QTableWidgetItem("UserIMSI"));
    table->setItem(9,0,new QTableWidgetItem("CorrelationId"));
    table->setItem(10,0,new QTableWidgetItem("MajorNumber"));
    table->setItem(11,0,new QTableWidgetItem("MinorNumber"));
    table->setItem(12,0,new QTableWidgetItem("Source Type"));
    table->setItem(13,0,new QTableWidgetItem("Total Consume"));
    table->setItem(14,0,new QTableWidgetItem("Source Info"));
    table->setItem(15,0,new QTableWidgetItem("Event"));
    table->setItem(16,0,new QTableWidgetItem("GroupID"));
    table->setItem(17,0,new QTableWidgetItem("ServiceID"));
    table->setItem(18,0,new QTableWidgetItem("OperatorId"));
    table->setItem(19,0,new QTableWidgetItem("Options Array"));

    table->resizeColumnsToContents();

    window->setCentralWidget(table);

    window->show();

    return app.exec();
}

Output of Table Widget

1 个答案:

答案 0 :(得分:2)

QString test;

test = "a";

// First Column
table->setItem(0,0,new QTableWidgetItem(a));

// Second Column
table->setItem(0,1,new QTableWidgetItem(a));

就像那样或:

// 100% working example
QVector< QString > string;
QVector< QString > value;

string.append( "Nr1" );
string.append( "Nr2" );
string.append( "Nr3" );
string.append( "Nr4" );

value.append( "1" );
value.append( "2" );
value.append( "3" );
value.append( "4" );

ui->twTable->setSortingEnabled( false );

for( int i = 0 ; i < string.size( ) && i < value.size( ) ; ++i )
{
    ui->twTable->insertRow( 0 );

    // First Column
    ui->twTable->setItem( 0 , 0 , new QTableWidgetItem( string[i] ) );

    // Second Column
    ui->twTable->setItem( 0 , 1 , new QTableWidgetItem( value[i] ) );
}

ui->twTable->setSortingEnabled( true );

arrayQStringList只是在没有附加的情况下以相同的方式工作。

数组:

QString str[5] = { "a" , "b" , "c" , "d" , "e" };

for( int i = 0 ; i < 5 ; ++i )
{
    ui->twTable->insertRow( 0 );

    // First Column
    ui->twTable->setItem( 0 , 0 , new QTableWidgetItem( str[i] ) );

    // Second Column
    ui->twTable->setItem( 0 , 1 , new QTableWidgetItem( str[i] + QString::number( i ) ) );
}

QStringList中:

QStringList str = { "a" , "b" , "c" , "d" , "e" };

for( int i = 0 ; i < 5 ; ++i )
{
    ui->twTable->insertRow( 0 );

    // First Column
    ui->twTable->setItem( 0 , 0 , new QTableWidgetItem( str[i] ) );

    // Second Column
    ui->twTable->setItem( 0 , 1 , new QTableWidgetItem( str[i] + QString::number( i ) ) );
}

以相反的顺序:

// Probably the output you want:
QString str[5] = { "a" , "b" , "c" , "d" , "e" };

for( int i = 4 ; i >= 0 ; --i )
{
    ui->twTable->insertRow( 0 );

    // First Column
    ui->twTable->setItem( 0 , 0 , new QTableWidgetItem( str[i] ) );

    // Second Column
    ui->twTable->setItem( 0 , 1 , new QTableWidgetItem( str[i] + QString::number( i ) ) );
}

当您使用固定数量的行时,请删除ui->twTable->insertRow( 0 )

旁注:我倾向于在插入项目时启用排序并启用它,因此输入时行不会自动排序到其他位置。