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. Read and Write from QTableWidget
Qt 6.11 is out! See what's new in the release blog

Read and Write from QTableWidget

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 789 Views 2 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
    #1

    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:

    ***window.cpp***
    
    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();
    
         filesTable->verticalHeader()->hide();
         filesTable->setShowGrid(false);
    
         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;
         gp.setModal(true);
         gp.exec();
    
    
    
    }
    
    QString Window:: getListofDiretories()
    {
        QString newListofDirectories = item ->text();
        return newListofDirectories;
    }
    
    
    ***newWindow.cpp** 
    ***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) :
        QDialog(parent),
        ui(new Ui::newWindow)
    {
        ui->setupUi(this);
    
    
    }
    
    newWindow::~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 !

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

      Hi,

      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.

      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

      • Login

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