Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTableWidget has the line -1?
QtWS25 Last Chance

QTableWidget has the line -1?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtablewidgetindexgettingcrazy
2 Posts 2 Posters 949 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LowEel
    wrote on 22 Apr 2017, 18:36 last edited by
    #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.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 22 Apr 2017, 21:46 last edited by
      #2

      Hi and welcome to devnet,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1

      2/2

      22 Apr 2017, 21:46

      • Login

      • Login or register to search.
      2 out of 2
      • First post
        2/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved