我必须在QTableWidget
中添加一行。以前我添加了一个列表大小的行,如下所示:
MyProgram::MyProgram( QWidget* parent )
: QDialog( parent )
, ui( new Ui::MyProgram )
{
ui->setupUi( this );
QStringList allFiles = QDir( "~/" ).entryList( QStringList << "*.txt" );
ui->myQTableWidget->setRowCount( allFiles.size() );
for( int cont = 0; cont < allFiles.size(); ++cont )
{
//insert values in my QTableWidget
}
}
但现在我不知道我将在QTableWidget
中显示多少个文件,因为我之前添加了验证。它是这样的:
MyProgram::MyProgram( QWidget* parent )
: QDialog( parent )
, ui( new Ui::MyProgram )
{
ui->setupUi( this );
QStringList allFiles = QDir( "~/" ).entryList( QStringList << "*.txt" );
for( int cont = 0; cont < allFiles.size(); ++cont )
{
bool ok = true;
try {
//try something
} catch( //exception )
{
ok = false;
}
if (ok) {
//insert values in my QTableWidget
}
}
}
如何在QTableWidget
中添加一行而不知道这将包含多少项?
答案 0 :(得分:1)
这有效:
MyProgram::MyProgram( QWidget* parent )
: QDialog( parent )
, ui( new Ui::MyProgram )
{
ui->setupUi( this );
QStringList allFiles = QDir( "~/" ).entryList( QStringList << "*.txt" );
for( int cont = 0; cont < allFiles.size(); ++cont )
{
bool ok = true;
try {
//try something
} catch( //exception )
{
ok = false;
}
if (ok) {
int row = ui->myQTableWidget->rowCount();
ui->myQTableWidget->insertRow( row );
// setItems
}
}
}