Important: Please read the Qt Code of Conduct -

Read and Write from QTableWidget

  • Greetings,

    Currently, i manged to print all the directories in the QTableWidget. After that , all the directories had been realeased out to the QTableWidget , I want to double clicked on selected row, and then from there, I'll get the information(directory) from selected row from QTableWidget and use that information that i retrieved in another class Here is my code:

    void Window::createFilesTable()
         filesTable = new QTableWidget(0,2);
         QStringList labels;
         labels << tr("File Name") << tr("Size");
         connect(filesTable, SIGNAL(cellDoubleClicked(int,int)),
                 this, SLOT(openFileOfItem(int,int)));
    void Window::openFileOfItem(int row, int /* column */)
         item = filesTable->item(row, 0);//is to check which cell had been activated in the table widget
            qDebug()<< item->text();
         newWindow gp;
    QString Window:: getListofDiretories()
        QString newListofDirectories = item ->text();
        return newListofDirectories;
    ***second class***
    #include "newWindow.h"
    #include "ui_newwindow.h"
    #include "window.h"
    #include <QTableWidget>
    #include <QTableWidgetItem>
    #include <QString>
    #include <QDebug>
    using namespace std;
    newWindow::newWindow(QWidget *parent) :
        ui(new Ui::newWindow)
        delete ui;
    QString newWindow:: getNewListofDirectories ()
        Window object = new Window();
        return object.getListofDiretories();
    void newWindow::on_pushButton_clicked()
        QString newAddress = getNewListofDirectories();
        qDebug()<< "Result:" <<newAddress;

    As you can see from the above codes, I have this function called openFileOfItem. well, this function is retrieve information from QTableWidget, and then from there ,i want to store the result in this function called getListofDiretories(), so that I can use the same information in second class. I got no errors nor warning when i try to compile them, but however didnt display any result that i want . i have no idea what is going on, it should behave as i expected . One more thing! the data type for filesTable is QTableWidget , and for item, the data type is QTableWidgetItem, i declare these variable at window.h , just so you know . someone here please tell me what is wrong with my implementation , it doesnt behave as I expected ! Thank you in advance !

  • Lifetime Qt Champion


    Your code's not very clear. In getNewListofDirectories, you create a widget, call a function from it and that's that. getListofDirectories returns a QString which is rather surprising based on the name of the function.

    On a side note, you also have a memory leak.

Log in to reply