QTableWidget has the line -1?



  • Hi All,

    I am pretty new to Qt development, so today I tried to do something easy, like showing a list of filenames into a QTableWidget.
    By itself the code is not so complex:

    #include <QApplication>
    #include <QDir>
    #include <QDebug>
    #include <QTableWidget>
    #include <QMainWindow>
    
    int main(int argc, char *argv[])
    {
         QApplication app(argc, argv);
         QMainWindow *finestra = new QMainWindow();
    
         QDir path("/etc/tinc/bosenet/hosts/");
        //two files here
         QStringList files = path.entryList(QDir::Files);
         qDebug() << files.count() << files ;
    
    
         QTableWidget*  tabella = new QTableWidget();
         tabella->setRowCount(files.count() );
         tabella->setColumnCount(1);
         tabella->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    
    
         int row = -1;
         foreach (QString filename , files  )
         {
             qDebug() << files.count() << row <<  filename ;
             tabella->setItem(row,1, new QTableWidgetItem(filename)  );
             row++;
         }
    
        finestra->setCentralWidget(tabella);
        finestra->show();
    
        return app.exec();
    }
    
    

    the problem is that, it only shows the 2 files when the initial row index is set to -1 . Which makes me crazy, since I was thinking it should start with 0.

    If I put the initial value to 0, it still works, just showing the first row empty, and the second one with the first file name.
    I put some qDebug lines (see the code) to see what's happening, and the debug was:

    2 ("RedOctober", "Vulvatron")
    2 -1 "RedOctober"
    2 0 "Vulvatron"
    

    ("RedOctober" and "Vulvatron" are the two files into the folder).

    it is not a problem by itself: once I know that this should start with -1, ok for me.

    I am using QT 5.5.1 on Linux Lubuntu g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609

    Is it a bug, or something?
    I am afraid If I write everything starting with -1 , then it will not compile on other systems.

    Thanks in advance.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You're on the wrong column, it should be 0.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.