qt connect cilds pushbutton - qt

I have
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void getData();
private:
Ui::MainWindow *ui;
Dialog *second;
};
and
class Dialog: public QDialog {
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0); QDialog * dialog;
QPushButton *pushButton;
QPushButton *pushButton_2;
};
and I can connect pushbuttons in class Dialog with function getData() in class MainWindow (Dialog is a child of class Mainwindow)
I tried
connect(*second->pushButton, SIGNAL(clicked()), this,
SLOT(getData()));
but I got
error: no matching function for call to
‘MainWindow::connect(QPushButton&, const char [11], MainWindow* const, const char [11])’
How do i connect them?

if the dialog is child of QMainWindow subclass (as it would be) you should have something like:
MainWindow::MainWindow(...)
{
....
m_dialog = new Dialog(this);// in .h file it is defined as: "Dialog *m_dialog;"
....
connect(m_dialog->pushButton, SIGNAL(clicked()), this,SLOT(getData()));
}
you don't have to write:
connect(*m_dialog->pushButton, SIGNAL(clicked()), this,SLOT(getData()));
take a look at this qt code:
Counter a, b;
QObject::connect(&a, SIGNAL(valueChanged(int)),
&b, SLOT(setValue(int)));
a.setValue(12); // a.value() == 12, b.value() == 12
b.setValue(48); // a.value() == 12, b.value() == 48
as you can see you must use pointer and not QObject.

Related

How to connect signal and slot in different classes in Qt?

I have two simple classes(class A and class B).
In a.h, I just declared a QPushButton:
QPushButton *testBtn = new QPushButton(this);
In b.h:
class B : public QMainWindow
{
Q_OBJECT
public:
explicit B(QWidget *parent = nullptr);
A testingA;
public slots:
void testing();
};
and b.cpp:
B::B(QWidget *parent) : QMainWindow(parent)
{
connect(testingA.testBtn, &QPushButton::clicked, this, &B::testing);
}
void B::testing()
{
qDebug() << "testing";
}
I am trying to connect the signal in class A to the slot in class B, but from the code I provide, it's not working.
So what is the right way to do it? Thanks
Edit:
According to PRIME's answer, I made a few changes.
In A's constructor, added:
connect(testBtn, &QPushButton::clicked, [this](){OnButtonClicked();});
to emit the own defined OnButtonClicked() signal;
and in B's construtor, changed to this:
connect(&testingA, &A::OnButtonClicked, this, &B::testing);
But when I clicked the button, the testing slot still not triggered.
Edit 2:
After doing some researches and trying a few times, I found that if I created B's object in A's constructor, and then connect A's signal to B's slot in A, it will work.
But I really can not figure out why I can not connect A's signal to B's slot in B.
This is what's in the main.cpp:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
B b;
A w;
w.show();
return a.exec();
}
Is that because of some reasons that A's object is out of scope in B?
Can someone tell me where I did wrong? Thanks so much.
Don't do it like this, hide your button in the class A, emit your own defined signal from class A lets call it OnButtonClicked.
Cascading code(inside A's c'tor):
connect(testBtn , &QPushButton::clicked, [this](){OnButtonClicked();});
You will also have to declare this new signal in class A now:
So class A must have folowing besides whatever it has right now:
class A
{
Q_OBJECT
signals:
void OnButtonClicked();
};
No special slot is needed since you are using a Lambda as a slot for the signal OnButtonClicked.
Connection in class B( do it in the c'tor ):
connect(testingA, &A::OnButtonClicked, this, &B::testing);
You can connect signal-to-signal in your sender object, for example widget containing the button:
class MyWidget : public QWidget
{
Q_OBJECT
QPushButton *pushButton;
public:
explicit MyWidget(QWidget *parent = nullptr) : QWidget(parent), pushButton(new QPushButton(this)) {
connect(pushButton, &QPushButton::click, this, &MyWidget::buttonClicked);
}
signals:
void buttonClicked();
public slots:
};
By the way you would normally send signals by using emit keyword, e.g.:
emit buttonClicked();
Then the consumer:
class TestObject : public QObject
{
Q_OBJECT
public:
explicit TestObject(QObject *parent = nullptr) : QObject(parent) { }
public slots:
void onButtonClicked() {
qDebug() << "clicked";
}
};
And connect both instances:
MyWidget widget;
TestObject to;
QObject::connect(&widget, &MyWidget::buttonClicked, &to, &TestObject::onButtonClicked);
in your class A you should use the signal testing of the class B, if you clicked on your button the OnButtonClicked function will be activated
A:
public slots:
void OnButtonClicked();
void A::OnButtonClicked()
{
...
emit testing(1);
}
B:
signals:
void testing(int level);
then to connect both you can do this
connect(startButton, &QPushButton::clicked, board, &A::OnButtonClicked);

How can I use replacedlg.ui in mainwindow.cpp?

I have written a project which includes a mainwindow and a replacedlg.ui. I want to use replacedlg.ui in mainwindow.cpp.
I'd like to write things like ui->button in mainwindow.cpp, but I can't.
Who can help me make this work?
The whole project is here.
Don't try to share the ui variable between classes. It is bad design. Instead add methods in your classes which will let you do what you need to do.
In your case where you want to send the text of your line edit from replaceDlg class to your MainWindow class, you should use signals and slots. Here is an example:
#include <QtWidgets>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = Q_NULLPTR) : QMainWindow(parent)
{
setCentralWidget(&text_edit);
}
public slots:
void addText(const QString &text)
{
text_edit.append(text);
}
private:
QTextEdit text_edit;
};
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = Q_NULLPTR) : QDialog(parent)
{
setLayout(new QHBoxLayout);
QPushButton *send_button = new QPushButton("Send");
layout()->addWidget(&line_edit);
layout()->addWidget(send_button);
connect(send_button, &QPushButton::clicked, this, &Dialog::sendButtonClicked);
}
signals:
void sendText(const QString &text);
private slots:
void sendButtonClicked()
{
emit sendText(line_edit.text());
accept();
}
private:
QLineEdit line_edit;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
Dialog d;
QObject::connect(&d, &Dialog::sendText, &w, &MainWindow::addText);
w.show();
d.show();
return a.exec();
}
#include "main.moc"

change text of QLabel in another class with button

I have ever post a question at there: change text in another class by button But it justs working when second class is create by first class. Now I have 2 classes, they're created sametime, so how to I connect with together? below is all my code, first class has a button, and second class has a label, I want to when user click on the button in first class, label in second class will be changed. They're put in stackWidget:
// file.h
#include <QWidget>
#include <QtGui>
class widgetA;
class widgetB;
class A : public QWidget
{
Q_OBJECT
public:
explicit A(QWidget *parent = 0);
private:
QComboBox* comboBox;
QStackedWidget* stackWidget;
widgetA *wa;
widgetB *wb;
};
class widgetA : public QWidget
{
Q_OBJECT
public:
widgetA(QWidget *parent = 0);
public slots:
void buttonClicked();
private:
QPushButton* button;
};
class widgetB : public QWidget
{
Q_OBJECT
public slots:
void labelChangeText(const QString);
public:
widgetB(QWidget *parent = 0);
QLabel* label;
};
And this's cpp file:
//file.cpp
#include "a.h"
A::A(QWidget *parent) :
QWidget(parent)
{
comboBox = new QComboBox(this);
comboBox->addItem(tr("Widget A"));
comboBox->addItem(tr("Widget B"));
wa = new widgetA(this);
wb = new widgetB(this);
stackWidget = new QStackedWidget(this);
stackWidget->addWidget(wa);
stackWidget->addWidget(wb);
stackWidget->setCurrentIndex(0);
connect(comboBox, SIGNAL(currentIndexChanged(int)), stackWidget, SLOT(setCurrentIndex(int)));
QVBoxLayout* layout = new QVBoxLayout;
layout->addWidget(comboBox);
layout->addWidget(stackWidget);
setLayout(layout);
}
widgetA::widgetA(QWidget *parent):
QWidget(parent)
{
button = new QPushButton(tr("Click"));
connect(button, SIGNAL(clicked()), this, SLOT(buttonClicked()));
QHBoxLayout* lay = new QHBoxLayout;
lay->addWidget(button);
setLayout(lay);
}
void widgetA::buttonClicked()
{
// what I have to do at there for call the function at widgetB class?
}
widgetB::widgetB(QWidget *parent):
QWidget(parent)
{
label = new QLabel("....");
QHBoxLayout* lay = new QHBoxLayout;
lay->addWidget(label);
setLayout(lay);
}
void widgetB::labelChangeText(const QString text)
{
label->setText(text);
}
P/S: sorry my english
You can either pass a pointer of widgetB object to widgetA, or you can connect a signal from widgetA to a slot in widgetB.
Here's an example of the signal/slot option:
// widgeta.h
signals:
void changeText(QString text);
-
// widgeta.cpp
void widgetA::buttonClicked()
{
emit changeText("button clicked");
}
-
//widgetb.h
public slots:
void labelChangeText(const QString & text);
-
// a.cpp
connect(wa, SIGNAL(changeText(QString)), wb, SLOT(labelChangeText(QString)));
What hinders you to call wb->labelChangeText("some string") in widgetA::buttonClicked()? You have a pointer to widgetB in class A. And labelChangeText is public in class B. Just that it is a slot does not mean that you cannot call it as a normal method.

QT no matching function for call to 'MainWindow::connect()

I have MainWindow and QNAMRedirect classes and I am trying to compile program but getting compiler error.
Here is QNAMRedirect class:
class QNAMRedirect : public QObject
{
Q_OBJECT
public:
explicit QNAMRedirect(QObject *parent = 0);
~QNAMRedirect();
signals:
public slots:
void doRequest();
void replyFinished(QNetworkReply* reply);
signals:
void finished(QString);
private:
QPointer<QNetworkAccessManager> _qnam;
QUrl _originalUrl;
QUrl _urlRedirectedTo;
QNetworkAccessManager* createQNAM();
};
and here is MainWindow class:
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_request_clicked();
private:
Ui::MainWindow *ui;
};
and i am trying to connect NAMRedirect::finished(QString) signal to QTextEdit widget in MainWindow this way:
void MainWindow::on_request_clicked()
{
QNAMRedirect urlGet(this);
QObject::connect(urlGet,SIGNAL(finished(QString)),ui->textEdit,SLOT(setText(QString)));
urlGet.doRequest();
}
but i am getting compiler error:
error: no matching function for call to 'MainWindow::connect(QNAMRedirect&, const char*, QTextEdit*&, const char*)'
how can i fix that?
The reason for the compile error is that the two objects you pass to the connect() function need to be pointers. So using &urlGet (instead of just urlGet) will fix your compile error. However, as soon as your function returns this object will go out of scope and be destroyed, so I suggest you change your function to look something more like this:
QNAMRedirect *urlGet = new QNAMRedirect( this )
QObject::connect(urlGet,SIGNAL(finished(QString)),ui->textEdit,SLOT(setText(QString)));
urlGet->doRequest();
You will, of course, need to take measure that you're not leaking memory here.

Qt4 parent pointer usage

I'm having some problem with understanding usage of parent pointer in QT4.
class firstClass : public QWidget
{
Q_OBJECT
public:
firstClass(QWidget *parent = 0);
~firstClass();
void doSomething();
private:
secondClass * myClass;
};
class secondClass : public QWidget
{
Q_OBJECT
public:
secondClass(QWidget *parent = 0);
void doSomethingElse();
};
I want to call doSomething() method while running doSomethingElse(). Is there any way to do it using parent pointer?
I tried parent->doSomething() but it doesn't work. It seems that Qt Creator is suggesting only methods from QObject class after parent->.
On the other hand I can't write it like secondClass(firstClass *parent = 0); - compilator returns error:
Thanks for any suggestions.
If you are positive that the parent of secondClass is always going to be firstClass then you can do this:
static_cast<firstClass *>(parent)->doSomething();
Alternatively you can use qobject_cast and check to make sure that parent is actually an instance of firstClass:
firstClass *myParent = qobject_cast<firstClass *>(parent);
if(myParent){
myParent->doSomething();
}
The more Qt-ish way to do this would be to use signals and slots, instead of trying to directly call a different function.
class firstClass : public QWidget
{
Q_OBJECT
public:
firstClass(QWidget *parent = 0);
~firstClass();
public slot:
void doSomething();
private:
secondClass * myClass;
};
class secondClass : public QWidget
{
Q_OBJECT
public:
secondClass(QWidget *parent = 0);
void doSomethingElse()
{
// ...
emit ( triggerDoSomething() );
// ...
}
signal:
void triggerDoSomething();
};
firstClass::firstClass(QWidget *parent) :
QWidget(parent), myClass(new secondClass(this))
{
// ...
bool connected = connect(myClass, SIGNAL(triggerDoSomething()),
SLOT(doSomething()));
assert( connected );
}

Resources