Solved QTableWidget Help
-
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.
-
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.
-
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!
-
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! -
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. -
Hahaa, there are only two options in topic tools. But i dont see marked as solved option available over there. thanks
-
@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) -
Got it! Thank you for the heads up ! I m new to this forum . peace out