crissilvaeng
crissilvaeng

Reputation: 165

Adding dynamically a row in a QTableWidget

I must adding a row in a QTableWidget. Previously I add all rows with a size of a list, like this:

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
    }
}

But now I can't know how many files I will show in QTableWidget, because I added a validation before. It like this:

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
        }
    }
}

How I can add a row in a QTableWidget without knowing how many items this will have?

Upvotes: 1

Views: 4156

Answers (1)

crissilvaeng
crissilvaeng

Reputation: 165

This works:

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
        }
    }
}

Upvotes: 1

Related Questions