How to pass data from one form to another in Qt? - qt

How can I pass data from one form to another in Qt?
I have created a QWidgetProgect -> QtGuiApplication, I have two forms currently. Now I want to pass data from one form to another.
How can I achieve that ?
Thanks.

Here are some options that you might want to try:
If one form owns the other, you can just make a method in the other and call it
You can use Qt's Signals and slots mechanism, make a signal in the form with the textbox, and connect it to a slot you make in the other form (you could also connect it with the textbox's textChanged or textEdited signal)
Example with Signals and Slots:
Let's assume that you have two windows: FirstForm and SecondForm. FirstForm has a QLineEdit on its UI, named myTextEdit and SecondForm has a QListWidget on its UI, named myListWidget.
I'm also assuming that you create both of the windows in the main() function of your application.
firstform.h:
class FistForm : public QMainWindow
{
...
private slots:
void onTextBoxReturnPressed();
signals:
void newTextEntered(const QString &text);
};
firstform.cpp
// Constructor:
FistForm::FirstForm()
{
// Connecting the textbox's returnPressed() signal so that
// we can react to it
connect(ui->myTextEdit, SIGNAL(returnPressed),
this, SIGNAL(onTextBoxReturnPressed()));
}
void FirstForm::onTextBoxReturnPressed()
{
// Emitting a signal with the new text
emit this->newTextEntered(ui->myTextEdit->text());
}
secondform.h
class SecondForm : public QMainWindow
{
...
public slots:
void onNewTextEntered(const QString &text);
};
secondform.cpp
void SecondForm::onNewTextEntered(const QString &text)
{
// Adding a new item to the list widget
ui->myListWidget->addItem(text);
}
main.cpp
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
// Instantiating the forms
FirstForm first;
SecondForm second;
// Connecting the signal we created in the first form
// with the slot created in the second form
QObject::connect(&first, SIGNAL(newTextEntered(const QString&)),
&second, SLOT(onNewTextEntered(const QString&)));
// Showing them
first.show();
second.show();
return app.exec();
}

You could also use pointers to access the QTextEdit (assuming that's what you're using) from the other form.
Following from Venemo's example (where FirstForm has the QTextEdit and SecondForm's the one you need to access the QTextEdit from):
firstform.h:
class FistForm : public QMainWindow
{
...
public:
QTextEdit* textEdit();
};
firstform.cpp:
QTextEdit* FirstForm::textEdit()
{
return ui->myTextEdit;
}
You can then access the QTextEdit's text in SecondForm with something like this (assuming your instance of FirstForm is called firstForm):
void SecondForm::processText()
{
QString text = firstForm->textEdit()->toPlainText();
// do something with the text
}

Related

How can my Dialog class know which element of the MainWindow has called it

I work with QT-Creator 4.9.1 based on Qt 5.12.3 and I am making a gui for a touch terminal. I have a stacked widget with multiple LineEdit widgets inside on different pages. The problem i have, is that my text from the keyboard should be shown inside the LineEdit of my MainWindow.
Question:
How can I determine which LineEdit called my touchkeyboard and how can i insert the pressed key inside my LineEdit in the MainWindow when my touchkeyboard dialog is modal?
Touch-Keyboard Dialog:
Example for one Stackwidget Page:
when creating QLineEdit you need set ID , Like that
#include <QLineEdit>
class MyLineEdit : public QLineEdit
{
Q_OBJECT
public:
MyLineEdit(int id = 0, QWidget* parent = nullptr);
int id() const;
private:
int m_id;
};
MyLineEdit::MyLineEdit(int id, QWidget *parent)
:QLineEdit (parent)
,m_id(id)
{
}
int MyLineEdit::id() const
{
return m_id;
}
after that in the slot you can find out through id which one QLineEdit gave the signal
connect(myLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(customSlot(const QString &)));
or also use lambda expression
connect(myLineEdit, &QLineEdit::textChanged,[this](const QString & txt){
// Touch-Keyboard Dialog
});

How can I connect a signal to QWidget::update slot?

I am using Qt5 with new signal/slot syntax.
I don't know why the following code doesn't work:
QWidget *widget = new QWidget();
connect(pipeline, &Pipeline::NewFrame, widget, &QWidget::update);
I get the error:
no matching member function for call to 'connect' why?
Pipeline class inherits from QObject and NewFrame signal has the same signature as QWidget::update
class Pipeline
: public QObject
{
Q_OBJECT
Q_DISABLE_COPY(Pipeline)
public:
Pipeline(QObject *parent);
signals:
void NewFrame();
};
I am using QtCreator on Arch Linux with g++.
TL;DR: The pipeline should be signaling an image, and the widget should have a SetImage method:
class Pipeline : public QObject {
Q_OBJECT // important
public:
Q_SIGNAL void NewFrame(const QImage &);
...
};
class Viewer : public QWidget {
Q_OBJECT // important
QImage m_image;
public:
Q_SLOT void SetImage(const QImage &image) {
m_image = image;
update();
}
...
};
This is how you'd be using it - note that Viewer knows nothing about Pipeline, because it shouldn't: it just shows new frames, wherever they come from.
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
Pipeline pipeline;
Viewer viewer;
QObject::connect(&pipeline, &Pipeline::NewFrame, &viewer, &Viewer::SetImage);
return app.exec();
}
Connecting anything to QWidget::update directly, especially from external sources, is usually a sign of bad design.
To satisfy your curiosity, you can use a lambda or qOverload to specify what you're connecting to, to fix the very error you're seeing - caused by ambiguity of the the type of the method pointer. Any of the following will work:
connect(…, widget, qOverload<>(&QWidget::update));
or
auto constexpr update = qOverload<>(&QWidget::update));
connect(…, widget, update);
or
connect(…, widget, [widget]{ widget->update(); });

signal points to slot in struct

I'd like a signal to be connected to a slot inside of a struct. My struct looks like:
//Header file
struct someStruct {
public:
int index;
public slots:
void someSlot();
};
QList<someStruct*> mListOfStructs;
and then I create a button that should forward its clicked() signal to the someSlot function.
//Source file
QPushButton *cmd = new QPushButton();
grd->addWidget(cmd, 3, 2, Qt::AlignCenter);
//grd is a QGridLayout somewhere inside the gui. I can see it and also the button.
now connection the clicked() event with the slot inside a specific struct does not work.
connect(cmd, SIGNAL(clicked()), mListOfStructs[3], SLOT(someSlot()));
some sources tell me that I have to add a metaObject or sth. I tried but it didn't work out. Maybe you know better.
I might use How to connect in Qt signal and slot in dynamically added buttons to get in slot index of added button? as workaround though.
your structure need the Q_Object meta attributes in order to emit signals and recieve slot events...
struct testStruct : public QObject
{
Q_OBJECT
public:
int index;
testStruct():index(0){}
public slots:
void someSlot()
{
qDebug() << "slot called!";
}
};
after that you can conected as usual:
testStruct *ts= new testStruct;
connect(this, SIGNAL(someSignal()), ts, SLOT(someSlot()));

QLabel click event using Qt?

I'm new in Qt and have a question.
I have QLabel and QLineEdit objects, and when QLabel text is clicked on, I want to set this text in QLineEdit.
Also I have read that QLabel has not clicked signal.
Can you explain how can I do this and write code for me ?!
Either style another type of QWidget such as a specific QPushButton to look like a QLabel and use its clicked() signal or inherit QLabel yourself and emit your own clicked() signal.
See this example:
https://wiki.qt.io/Clickable_QLabel
If you choose the latter option you can pass the text in the signal. Then connect the necessary signals/slots up between the QLabel and the QLineEdit like so:
QObject::connect(&label, SIGNAL(clicked(const QString& text)),
&lineEdit, SLOT(setText(const QString& text)));
A simple way to accomplish that, without a need for any subclassing, is a signal source that monitors the events on some object and emits relevant signals:
// main.cpp - this is a single-file example
#include <QtWidgets>
class MouseButtonSignaler : public QObject {
Q_OBJECT
bool eventFilter(QObject * obj, QEvent * ev) Q_DECL_OVERRIDE {
if ((ev->type() == QEvent::MouseButtonPress
|| ev->type() == QEvent::MouseButtonRelease
|| ev->type() == QEvent::MouseButtonDblClick)
&& obj->isWidgetType())
emit mouseButtonEvent(static_cast<QWidget*>(obj),
static_cast<QMouseEvent*>(ev));
return false;
}
public:
Q_SIGNAL void mouseButtonEvent(QWidget *, QMouseEvent *);
MouseButtonSignaler(QObject * parent = 0) : QObject(parent) {}
void installOn(QWidget * widget) {
widget->installEventFilter(this);
}
};
The emit keyword is an empty macro, Qt defines it as follows:
#define emit
It is for use by humans as a documentation aid prefix only, the compiler and moc ignore it. As a documentation aid, it means: the following method call is a signal emission. The signals are simply methods whose implementation is generated for you by moc - that's why we have to #include "main.moc" below to include all the implementations that moc has generated for the object class(es) in this file. There's otherwise nothing special or magical to a signal. In this example, you could look in the build folder for a file called main.moc and see the implementation (definition) of void MouseButtonSignaler::mouseButtonEvent( .. ).
You can then install such a signaler on any number of widgets, such as a QLabel:
int main(int argc, char ** argv) {
QApplication app(argc, argv);
MouseButtonSignaler signaler;
QWidget w;
QVBoxLayout layout(&w);
QLabel label("text");
QLineEdit edit;
layout.addWidget(&label);
layout.addWidget(&edit);
signaler.installOn(&label);
QObject::connect(&signaler, &MouseButtonSignaler::mouseButtonEvent,
[&label, &edit](QWidget*, QMouseEvent * event) {
if (event->type() == QEvent::MouseButtonPress)
edit.setText(label.text());
});
w.show();
return app.exec();
}
#include "main.moc"
You need to create one Custom Label class, which will inherit QLabel. Then you can use MouseButtonRelease event to check clicking of Label and emit your custom signal and catch in one SLOT.
Your .h file will be as below:
class YourLabelClass : public QLabel{
signals:
void myLabelClicked(); // Signal to emit
public slots:
void slotLabelClicked(); // Slot which will consume signal
protected:
bool event(QEvent *myEvent); // This method will give all kind of events on Label Widget
};
In your .cpp file, your constructor will connect signal & slot as below :
YourLabelClass :: YourLabelClass(QWidget* parent) : QLabel(parent) {
connect(this, SIGNAL(myLabelClicked()), this, SLOT(slotLabelClicked()));
}
Remaining event method and SLOT method will be implemented as below:
bool YourLabelClass :: event(QEvent *myEvent)
{
switch(myEvent->type())
{
case(QEvent :: MouseButtonRelease): // Identify Mouse press Event
{
qDebug() << "Got Mouse Event";
emit myLabelClicked();
break;
}
}
return QWidget::event(myEvent);
}
void YourLabelClass :: slotLabelClicked() // Implementation of Slot which will consume signal
{
qDebug() << "Clicked Label";
}
For Changing a Text on QLineEdit, you need to create a Custom Class and share object pointer with custom QLabel Class. Please check test code at this link
In the above example the header needs Q_OBJECT:
class YourLabelClass : public QLabel{
Q_OBJECT
signals:

How to pass a widget variable to slot?

I am a QT beginner and I am writing a GUI app that has two QPushButtons and two QTextEdits. When button 1 is clicked, I want only QTextEdit 1 to show something, when button 2 is clicked, I want only QTextEdit 2 to show something, below is my code, but it doesn't work because it seems that I cannot pass parameters to slot method....
class EventProcessor: public QObject {
Q_OBJECT
public slots:
void PopulateEditTest(QTextEdit *textEdit, QString text)
{
textEdit->setText(text);
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *window = new QWidget;
QTextEdit *result1 = new QTextEdit();
QTextEdit *result2 = new QTextEdit();
QPushButton *btFacility1 = new QPushButton("Facility 1");
QPushButton *btFacility2 = new QPushButton("Facility 2");
EventProcessor eventprocessor;
QObject::connect(btFacility1, SIGNAL(clicked()), &eventprocessor, SLOT(PopulateEditTest
(result1, "textEdit1")));
QObject::connect(btFacility2, SIGNAL(clicked()), result, SLOT(PopulateEditTest
(result2, "textEdit2")));
..........
return app.exec();
}
I am wondering if there is a similar way to get this working? Thank you for your answer.
You are correct in that you cannot specify arguments to be passed to a slot when you call QObject::connect. All you are providing are function definitions, not function calls.
The simplest way to accomplish what you're trying to do would be to add a couple of slots to your event processor class:
class EventProcessor: public QObject {
Q_OBJECT
public slots:
void PopulateEditTest(QTextEdit *textEdit, QString text) { ... }
void button1Clicked() {
PopulateEditTest(dynamic_cast<QTextEdit*>(sender()), "textEdit1");
}
void button2Clicked() {
PopulateEditTest(dynamic_cast<QTextEdit*>(sender()), "textEdit1");
}
};
...
QObject::connect(btFacility1, SIGNAL(clicked()), &eventprocessor, SLOT(button1clicked()));
QObject::connect(btFacility2, SIGNAL(clicked()), &eventprocessor, SLOT(button2Clicked()));
There are various reasons why it is generally considered to be "evil" to use the QObject::sender() function in this manner, but it is the simplest way to accomplish what you're trying to do without rewriting all of your code, which would be pointless me of to do without knowing what your long term goals are.

Resources