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. [Solved]How to retrieve data from the QTableWidget ?
Forum Updated to NodeBB v4.3 + New Features

[Solved]How to retrieve data from the QTableWidget ?

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 5.0k Views 1 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.
  • M Offline
    M Offline
    mahmoud899
    wrote on last edited by
    #1

    Hello, I am a beginner in the Qt world. I have written a very simple program. In the program I have a button and a label, the label is set to the number 0. I want when I push the button the label to be reset to another number. I have written the following code
    @
    #include <QApplication>
    #include <QLabel>
    #include <QPushButton>
    #include <QGridLayout>

    void change( QLabel *label )
    {
    label->setText(QString::number('32'));
    }

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    QWidget *window = new QWidget;
    
    QGridLayout *layout = new QGridLayout(window);
    
    QPushButton *button = new QPushButton("+");
    QLabel *value = new QLabel("0");
    
    layout->addWidget(button);
    layout->addWidget(value);
    
    QObject::connect(button, SIGNAL(clicked()), value, SLOT(change(label)));
    
    window->resize(200,200);
    window->setWindowTitle("Project");
    window->show();
    
    return a.exec(&#41;;
    

    }
    @

    When I run the code and press the push button nothing happens. Any suggestions how I fix that code. Please keep it simple as possible nothing fancy.

    Thank You

    Mahmoud Ramy

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mahmoud899
      wrote on last edited by
      #2

      Small change in the code, instead of @SLOT(change(label))@

      use this code
      @SLOT(change(value))@

      Mahmoud Ramy

      1 Reply Last reply
      0
      • M Offline
        M Offline
        melghawi
        wrote on last edited by
        #3

        Hello and welcome to Qt.

        The reason why nothing happens is because you are connecting the button's clicked signal to the value's change slot which is not defined as a slot for the QLabel. Have a look at "this":http://qt-project.org/doc/qt-4.8/signalsandslots.html.

        But basically you need to do something like this:

        @
        class CustomLabel : public QLabel
        {
        Q_OBJECT
        public slots:
        void change()
        {
        setText(QString::number('32'));
        }
        }
        @

        Then in your main.cpp you need to do something like this:

        @
        int main(int argc, char *argv[])
        {
        QApplication a(argc, argv);

        QWidget *window = new QWidget;
        
        QGridLayout *layout = new QGridLayout(window);
        
        QPushButton *button = new QPushButton("+");
        CustomLabel *value = new CustomLabel("0");
        
        layout->addWidget(button);
        layout->addWidget(value);
        
        QObject::connect(button, SIGNAL(clicked()), value, SLOT(change()));
        
        window->resize(200,200);
        window->setWindowTitle("Project");
        window->show();
        
        return a.exec&#40;&#41;;
        

        }
        @

        Now the value object has a change slot defined.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mahmoud899
          wrote on last edited by
          #4

          communication.h
          @
          #ifndef COMMUNICATION_H
          #define COMMUNICATION_H

          #include <QApplication>
          #include <QPushButton>
          #include <QLabel>
          #include <QWidget>

          class myWindow : public QWidget
          {
          Q_OBJECT

          public:
          myWindow(QWidget *parent = 0);

          private slots:
          void onChange();

          private:
          QLabel *label;
          };

          #endif // COMMUNICATION_H
          @

          communication.cpp
          @
          #include "communication.h"

          myWindow::myWindow(QWidget *parent)
          :QWidget(parent)
          {
          QPushButton *button = new QPushButton("Change", this);
          button->setGeometry(40,40,40,40);

          label = new QLabel("0", this);
          label->setGeometry(30,40,30,40);
          
          connect(button, SIGNAL(clicked()), this, SLOT(change()));
          

          }

          void myWindow::onChange()
          {
          label->setText("Hello World");
          }
          @

          main.cpp
          @
          QApplication a(argc, argv);

          myWindow window;

          myWindow.setWindowTitle("Communicate");
          myWindow.show();

          return a.exec();
          @

          I have written this code just to change a QLabel with a QPushButton. Now I have 2 questions,

          1)When I run that code on Ubuntu Linux I get the following error:
          "undefined reference to 'vtable for myWindow' and when I run it on Windows XP I don't get any errors and the program executes fine. How can I solve that problem on Ubuntu Linux?
          2) The second question is, isn't there any other simple code to change a QLabel with a QPushButton?

          Please put in mind that I am a beginner, so please keep your answers as simple as you can.

          Thank You

          Mahmoud Ramy

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

            Hi,

            For question 1: rerun qmake, or simply do a complete rebuild of your project. This is generally an error coming from add Q_OBJECT after having built the class one time.

            1. It's already a simple way of doing what you want. The only other is to add the slot to derived QLabel class

            Hope it helps

            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
            • M Offline
              M Offline
              mahmoud899
              wrote on last edited by
              #6

              For the problem that I have been having, building my project and an error occurs, I have rebuild the whole project and run qmake again and everything worked fine.

              Thank You

              Mahmoud Ramy

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

                You're welcome,

                Since it's all good now, don't forget to update the thread's title to solved so other forum users may also know :)

                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
                • M Offline
                  M Offline
                  mahmoud899
                  wrote on last edited by
                  #8

                  Hello I have a couple of questions.

                  1. When I want to post a new question do I start a new thread or continue on the thread I have created?

                  2. When my problem is solved, how can I change the threads title to solved?

                  3. I am experimenting with QTableWidget. I have made a QTableWidget and I have inserted into one of the cells a number. Now I want to retrieve what is in the cell?

                  @
                  #include <QApplication>
                  #include <QHBoxLayout>
                  #include <QTableWidget>
                  #include <QTextStream>

                  int main(int argc, char *argv[])
                  {
                  QApplication a(argc, argv);

                  QWidget window;
                  QTableWidgetItem *itemInTable;
                  QTextStream output(stdout);
                  
                  QTableWidget *table = new QTableWidget(20,20);
                  
                  QHBoxLayout *layout = new QHBoxLayout(&window);
                  
                  layout->addWidget(table);
                  
                  table->setItem(1,0, new QTableWidgetItem("30"));
                  
                  itemInTable = table->item(1,0);
                  
                  output << &itemInTable << endl;
                  
                  window.resize(300,300);
                  window.show();
                  
                  return a.exec&#40;&#41;;
                  

                  }
                  @

                  when I execute that code I get the reference of the cell not what is inside the cell. Please keep your answers as simple as possible. Thank You

                  Mahmoud Ramy

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    john_god
                    wrote on last edited by
                    #9

                    Hi mahmoud Welcome a board

                    1. You should start a new question, if it's unrelated with the previous one
                    2. There's a edit button somewhere, I think near the title

                    @output << itemInTable->text() << endl;@

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mahmoud899
                      wrote on last edited by
                      #10

                      Hello, sorry for the mess, when I have a new question I'll start a new thread. The solution for the QTableWidgetItem worked. Thank You

                      Mahmoud Ramy

                      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