[Solved]How to retrieve data from the QTableWidget ?
-
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();
}
@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
-
Small change in the code, instead of @SLOT(change(label))@
use this code
@SLOT(change(value))@ -
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();
}
@Now the value object has a change slot defined.
-
communication.h
@
#ifndef COMMUNICATION_H
#define COMMUNICATION_H#include <QApplication>
#include <QPushButton>
#include <QLabel>
#include <QWidget>class myWindow : public QWidget
{
Q_OBJECTpublic:
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
-
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.
- 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
-
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
-
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 :)
-
Hello I have a couple of questions.
-
When I want to post a new question do I start a new thread or continue on the thread I have created?
-
When my problem is solved, how can I change the threads title to solved?
-
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();
}
@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
-
-
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