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 Help
Qt 6.11 is out! See what's new in the release blog

QTableWidget Help

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 2.3k Views 3 Watching
  • 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.
  • QT_QT_QTQ Offline
    QT_QT_QTQ Offline
    QT_QT_QT
    wrote on last edited by QT_QT_QT
    #1

    Greetings,

    Currently, i am developing a mini program. This mini program that i m working on is to search for a specific folder in a directory, and then list all the sub-directory that contain the specific folder in a QTableWidget. If there are many sub-directories, my program will allow the user to choose one of the sub-directory in order to proceed to next task(After the checking opreration had been performed, it will return a list of sub-diretories in QTableWidget)

    ***window.h***
    
    #ifndef WINDOW_H
    #define WINDOW_H
    
    #include <QDialog>
    #include <QPushButton>
    #include <QLabel>
    #include <QComboBox>
    #include <QStringList>
    #include <QHBoxLayout>
    #include <QGridLayout>
    #include <QTableWidget>
    #include <QDir>
    #include <QProgressDialog>
    #include <QTextStream>
    #include <QDesktopServices>
    #include <QFileDialog>
    #include <QHeaderView>
    #include <QApplication>
    #include <QVector>
    
    
    using namespace std;
    
    class Window : public QDialog
    {
        Q_OBJECT
    
    public:
        Window(QWidget *parent = 0);
        ~Window();
    
    private slots:
         void browse();
         void find();
         void openFileOfItem(int row, int column);
    
     private:
         QStringList findFiles(const QStringList &files, const QString &text);
         void showFiles(const QVector<QDir> ListofDirectories );
         QPushButton *createButton(const QString &text, const char *member);
         QComboBox *createComboBox(const QString &text = QString());
         void createFilesTable();
         void getFileDirectory(QDir ListOfDirectory);
    
         QComboBox *fileComboBox;
         QComboBox *textComboBox;
         QComboBox *directoryComboBox;
         QLabel *fileLabel;
         QLabel *textLabel;
         QLabel *directoryLabel;
         QLabel *filesFoundLabel;
         QPushButton *browseButton;
         QPushButton *findButton;
         QTableWidget *filesTable;
    
         QDir currentDir;
     };
    
    
    
    
    #endif // WINDOW_H
    
    
    ***window.cpp**
    
    void Window::find()
     {
         filesTable->setRowCount(0);
    
         
         QString path = directoryComboBox->currentText();
       
              QDir::Filters df = QDir::Dirs |QDir::NoDotAndDotDot | QDir::Hidden;
              QDirIterator::IteratorFlag dff = QDirIterator::Subdirectories;
    
              QString root = path;
              QDirIterator it(root,df,dff);
              QVector<QDir> ListofDirectory ;
    
    
              while(it.hasNext())
             {
                  QString str2 = it.next();
    
    
                  if((str2.contains("newFolder"))
                  {
    
                      QDir drty = it.filePath();
                      drty.cdUp();
    
                      ListofDirectory.push_back(drty);
    
    
                   }
            }
            showFiles(ListofDirectory);
    }
    
    void Window::showFiles(const QVector<QDir> ListofDirectories)
    {
        for (int i = 0; i < ListofDirectories.size(); ++i) {
            QFile file(ListofDirectories.pop_back());
          
    
            QTableWidgetItem *fileNameItem = new QTableWidgetItem(ListofDirectories.pop_back());
            fileNameItem->setFlags(fileNameItem->flags() ^ Qt::ItemIsEditable);
          
            int row = filesTable->rowCount();
            filesTable->insertRow(row);
            filesTable->setItem(row, 0, fileNameItem);
        
        }
        
    }
    
    void Window::createFilesTable()
     {
         filesTable = new QTableWidget(0,2);
         filesTable->setSelectionBehavior(QAbstractItemView::SelectRows);
    
         QStringList labels;
         labels << tr("File Name") << tr("Size");
         filesTable->setHorizontalHeaderLabels(labels);
         //filesTable->horizontalHeader()->setSectionResizeMode();
    
         filesTable->verticalHeader()->hide();
         filesTable->setShowGrid(false);
    
         connect(filesTable, SIGNAL(cellActivated(int,int)),
                 this, SLOT(openFileOfItem(int,int)));
     }
    
    void Window::openFileOfItem(int row, int /* column */)
     {
         QTableWidgetItem *item = filesTable->item(row, 0);
    
         QDesktopServices::openUrl(QUrl::fromLocalFile(currentDir.absoluteFilePath(item->text())));
     }
    
    

    So, find() this function is actually search for the specific folder, after that , stored all the sub-directories that contains that specific folder in QVector. Right now, I wish to display all the sub-directories in a QTableWidget. Not only that, after all the sub-directories had been successfully listed out in the QTableWidget, user are able to select one of the sub-directories, like something choosing the subdirectories among the other directories, and use that sub directories to proceed to next task . FYI, abvoe code will give you these error when i try to compile them : void QVector<QDir>::pop_back(void);cannot convert 'this' pointer form const QVector<QDIr> to QVector<QDir>. Can anyone here tell me am i doing the right way ? if not then please tell me what should i do in order to meet my requirement. Thank you.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You are trying to remove an element of constant QVector. As an additional not, pop_back returns void which is probably not what you are looking for.

      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
      0
      • QT_QT_QTQ Offline
        QT_QT_QTQ Offline
        QT_QT_QT
        wrote on last edited by QT_QT_QT
        #3

        Hi folks,

        This is the solution for this problem . Feel free to take a look ! Thanks !

        ***window.h***
        
        #ifndef WINDOW_H
        #define WINDOW_H
        
        #include <QDialog>
        #include <QPushButton>
        #include <QLabel>
        #include <QComboBox>
        #include <QStringList>
        #include <QHBoxLayout>
        #include <QGridLayout>
        #include <QTableWidget>
        #include <QDir>
        #include <QProgressDialog>
        #include <QTextStream>
        #include <QDesktopServices>
        #include <QFileDialog>
        #include <QHeaderView>
        #include <QApplication>
        #include <QVector>
        
        
        using namespace std;
        
        class Window : public QDialog
        {
            Q_OBJECT
        
        public:
            Window(QWidget *parent = 0);
            ~Window();
        
        private slots:
             void browse();
             void find();
             void openFileOfItem(int row, int column);
        
         private:
             QStringList findFiles(const QStringList &files, const QString &text);
             void showFiles( QVector<QString> ListofDirectories );
             QPushButton *createButton(const QString &text, const char *member);
             QComboBox *createComboBox(const QString &text = QString());
             void createFilesTable();
             void getFileDirectory(QDir ListOfDirectory);
        
             QComboBox *fileComboBox;
             QComboBox *textComboBox;
             QComboBox *directoryComboBox;
             QLabel *fileLabel;
             QLabel *textLabel;
             QLabel *directoryLabel;
             QLabel *filesFoundLabel;
             QPushButton *browseButton;
             QPushButton *findButton;
             QTableWidget *filesTable;
        
             QDir currentDir;
         };
        
        
        
        
        #endif // WINDOW_H
        
        
        
        ***window.cpp**
        
        void Window::find()
         {
             filesTable->setRowCount(0);
        
         
             QString path = directoryComboBox->currentText();
        
        
                  QDir::Filters df = QDir::Dirs |QDir::NoDotAndDotDot | QDir::Hidden;
                  QDirIterator::IteratorFlag dff = QDirIterator::Subdirectories;
        
                  QString root = path;
                  QDirIterator it(root,df,dff);
                  QVector<QString> ListofDirectory ;
        
        
                  while(it.hasNext())
                 {
                      QString str2 = it.next();
        
        
                      if((str2.contains("NewFolder"))
                      {
        
                          QDir drty = it.filePath();
                          drty.cdUp();
        
                          ListofDirectory.push_back(drty.absolutePath());
        
        
                       }
                }
                showFiles(ListofDirectory);
        }
        
        
        ***window.cpp***
        
        void Window::showFiles( QVector<QString> ListofDirectories)
        {
            for (int i = 0; i < ListofDirectories.size(); ++i) {
                //QFile file(currentDir.absoluteFilePath(files(i)));
                //QStringList tu = ListofDirectories.pop_back();
                   QFile file(ListofDirectories.at(i));
        
                qint64 size = QFileInfo(file).size();
        
                QTableWidgetItem *fileNameItem = new QTableWidgetItem(ListofDirectories.at(i));
                fileNameItem->setFlags(fileNameItem->flags() ^ Qt::ItemIsEditable);
                QTableWidgetItem *sizeItem = new QTableWidgetItem(tr("%1 KB")
                                                     .arg(int((size + 1023) / 1024)));
                sizeItem->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
                sizeItem->setFlags(sizeItem->flags() ^ Qt::ItemIsEditable);
        
                int row = filesTable->rowCount();
                filesTable->insertRow(row);
                filesTable->setItem(row, 0, fileNameItem);
                filesTable->setItem(row, 1, sizeItem);
            }
            filesFoundLabel->setText(tr("%1 file(s) found").arg(ListofDirectories.size()) +
                                     (" (Double click on a file to open it)"));
        }
        

        There are two problems that i encountered, first one is the type of VEctor, initially , i declare the vector as QVector<QDirs>, now , i changed to QString. The second problem that i faced was getting information from a vector, initially i m using pop_back method to retirieve information from vector class, and then i changed to .at(i). Feel free to take a look ! Thank you!

        1 Reply Last reply
        1
        • QT_QT_QTQ Offline
          QT_QT_QTQ Offline
          QT_QT_QT
          wrote on last edited by QT_QT_QT
          #4

          HI folks,

          This topic is closed, i wanted to put this topic as "solved", however i cannot find this bloody hell "solved" button.
          Once again, this topic is closed! thank you!

          mrjjM 1 Reply Last reply
          1
          • QT_QT_QTQ QT_QT_QT

            HI folks,

            This topic is closed, i wanted to put this topic as "solved", however i cannot find this bloody hell "solved" button.
            Once again, this topic is closed! thank you!

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #5

            @QT_QT_QT

            Well +1 for trying.
            Its on first post. The "topic tool" button. (under topic)
            First Click "ask as question"
            Then click the Mark as Solved.

            QT_QT_QTQ 1 Reply Last reply
            0
            • mrjjM mrjj

              @QT_QT_QT

              Well +1 for trying.
              Its on first post. The "topic tool" button. (under topic)
              First Click "ask as question"
              Then click the Mark as Solved.

              QT_QT_QTQ Offline
              QT_QT_QTQ Offline
              QT_QT_QT
              wrote on last edited by
              #6

              @mrjj

              Hahaa, there are only two options in topic tools. But i dont see marked as solved option available over there. thanks

              mrjjM 1 Reply Last reply
              0
              • QT_QT_QTQ QT_QT_QT

                @mrjj

                Hahaa, there are only two options in topic tools. But i dont see marked as solved option available over there. thanks

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #7

                @QT_QT_QT
                hehe well u are not the first to not see it :)
                So the FIRST post. Under that. There is not
                as Mark as Solved ? (inside the button)

                1 Reply Last reply
                0
                • QT_QT_QTQ Offline
                  QT_QT_QTQ Offline
                  QT_QT_QT
                  wrote on last edited by
                  #8

                  @mrjj

                  Got it! Thank you for the heads up ! I m new to this forum . peace out

                  1 Reply Last reply
                  0

                  • Login

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