connect QPushButton to a function inside a the same class - c++

I want to connect QPushButton to a function inside the same class. the class is just a QGridLayout that making ui via code. I don't know what to do. actually I don't know what to put in for reciever argument in QObject::connect.
I don't want to use bunch of h and cpp files so that the code be as simple and readable as possible.
here is my main.cpp file:
#include <QApplication>
#include <QtWidgets>
#include <iostream>
using namespace std;
class ConnectPage
{
public:
void login_clicked(){
cout<<"login pressed"<<endl;
}
QGridLayout *main_layout = new QGridLayout;
// user interface Labels
QLabel *username_label = new QLabel("username");
QLabel *password_label = new QLabel("password");
QLabel *host_label = new QLabel("host");
QLabel *port_label = new QLabel("port");
QLabel *status_bar = new QLabel;
// user interface lineedits
QLineEdit *username_lineedit = new QLineEdit;
QLineEdit *password_lineedit = new QLineEdit;
QLineEdit *host_lineedit = new QLineEdit;
QLineEdit *port_lineedit = new QLineEdit;
// user interface buttons
QPushButton *login = new QPushButton("Connect");
QPushButton *reset = new QPushButton("Clear form");
ConnectPage()
{
QObject::connect(login, SIGNAL(clicked()), &app, SLOT(login_clicked()));
main_layout->addWidget(username_label,0,0,1,1);
main_layout->addWidget(username_lineedit,0,1,1,1);
main_layout->addWidget(password_label,1,0,1,1);
main_layout->addWidget(password_lineedit,1,1,1,1);
main_layout->addWidget(host_label,2,0,1,1);
main_layout->addWidget(host_lineedit,2,1,1,1);
main_layout->addWidget(port_label,3,0,1,1);
main_layout->addWidget(port_lineedit,3,1,1,1);
main_layout->addWidget(reset,4,0,1,1);
main_layout->addWidget(login,4,1,1,1);
main_layout->addWidget(status_bar,5,0,1,2);
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *window = new QWidget;
ConnectPage start_connecting;
QGridLayout *main_layout = start_connecting.main_layout;
window->setLayout( main_layout );
window->setWindowTitle("Login Page");
window->show();
return app.exec();
}
and here is .pro file:
QT += core gui widgets
SOURCES += \
main.cpp \
HEADERS += \
any help that regards only these two files would be appreciated.

If you are using Qt <= 15.2, i think you should start at reading Qt resources with signals and slots. link
Simple answer is that:
your class should inherit from Qobject,and you should put QObject makro in top and private section of this class.
class example : public QObject
{
Q_OBJECT
public: ...
public slots: ...
private: ...
};
Function that is slot, should be in public slots: section of a class. Like:
public slots:
void login_clicked(){
cout<<"login pressed"<<endl;
}
You could capture this function in class constructor like this:
connect(login, &QPushButton::pressed, this, &ConnectPage::login_clicked);
And you may use a lambda expression in connect.
connect(login, &QPushButton::pressed, [&](){
cout<<"login pressed"<<endl;
});
And for last word. If you want to use syntax that you have now, you need to remember about function signatures. From documentation:
"The rule about whether to include arguments or not in the SIGNAL() and SLOT() macros, if the arguments have default values, is that the signature passed to the SIGNAL() macro must not have fewer arguments than the signature passed to the SLOT() macro."
So this syntax should work:
connect(login, SIGNAL( clicked(bool)), this, SLOT(login_clicked()));
But I don't like this syntax.
Best regards!

Related

Little issue with 'this' (invalid use of 'this' in a non-member function)

When I have solved this I am finally done with my prog :D As always, a model of the problem is below. I get the invalid use of 'this' in a non-member function error. It seems to me I have done everything correctly: I have moved the class outside the main function and I have also not forgotten the Q_OBJECT macro... Could anybody please help me here and please mind that I am new to OOP. Thank you!
#include <QtGui>
#include <QtCore>
class MyObject : public QObject
{
Q_OBJECT
public:
QTextEdit text;
QString a;
public slots:
void onClicked() {
text.setText(a);
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget mw;
mw.setWindowTitle("Main Window");
mw.resize(400, 400);
mw.show();
QLabel label ("Enter something:", &mw);
label.setAlignment(Qt::AlignHCenter);
label.show();
QLineEdit line (&mw);
line.show();
QString a = line.text();
QTextEdit text (&mw);
text.show();
QPushButton btn ("Convert", &mw);
QObject::connect(
&btn,
SIGNAL(clicked()),
this, /* the compiler keeps complaining... */
SLOT(onClicked()));
btn.show();
QVBoxLayout layout_mw;
layout_mw.addWidget(&label);
layout_mw.addWidget(&line);
layout_mw.addWidget(&btn);
layout_mw.addWidget(&text);
mw.setLayout(&layout_mw);
return app.exec();
}
You can't use this outside a non-static member function.
It seems you want to connect the clicked() signal to the onClicked() function on an instance of MyObject. That means you need to first of all create an instance of the MyObject class. Then use a pointer to that object as the receiver of the signal:
MyObject my_object;
QObject::connect(
&btn,
SIGNAL(clicked()),
&my_object,
SLOT(onClicked()));
Be careful though, because the member variables in MyObject have nothing related with the local variables with the same name in the main function.
From my example code above, my_object.text is a totally different variable from text. The same with my_object.a and a, of course.
As shown in a comment to your question, there are better ways to do what you want, without the need to create the MyObject class.

QT C++ - Signals and Slots: "No such slot QLabel..." even the SLOT function is existing in my class

So I am trying to learn Qt Framework with C++. I am in the middle of understanding signals and slots but I am having a hard time creating a customized slot. So as I follow some tutorials, my program ended up with the following error:
QObject::connect: No such slot QLabel::updateMessage() in main.cpp:28
Where updateMessage() is declared as a public slot inside my Test class
The following are some snippets from my code:
Test.h
class Test : public QObject
{
Q_OBJECT
public:
Test(void);
~Test(void);
void setMessage(char *tMsg);
char* getMessage();
QWidget *window;
QGridLayout *layout;
QLabel *lblMsg;
QPushButton *btnShow;
public slots:
void updateMessage();
private:
char msg[80];
QString str;
};
main.cpp
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Test t;
t.window->setWindowTitle("Testing Qt");
t.window->setLayout(t.layout);
t.window->show();
return app.exec();
}
Test::Test(void)
{
window = new QWidget;
lblMsg = new QLabel;
btnShow = new QPushButton("Show message");
connect(btnShow,SIGNAL(clicked()),lblMsg,SLOT(updateMessage()));
layout = new QGridLayout;
layout->addWidget(lblMsg);
layout->addWidget(btnShow);
char str1[] = "Hello, Qt World!";
setMessage(str1);
}
Test::~Test(void)
{
}
void Test::setMessage(char *tMsg)
{
memcpy(msg, tMsg, sizeof(msg));
}
char* Test::getMessage()
{
return msg;
}
void Test::updateMessage()
{
string strMsg(getMessage());
QString qstr = QString::fromStdString(strMsg);
lblMsg->setText(qstr);
delete msg;
}
HelloQtWorld.pro
######################################################################
# Automatically generated by qmake (3.0) Tue Nov 15 00:30:22 2016
######################################################################
TEMPLATE = app
TARGET = HelloQtWorld
INCLUDEPATH += .
# Input
HEADERS += stdafx.h Test.h
SOURCES += anotherClass.cpp \
main.cpp \
stdafx.cpp \
GeneratedFiles/qrc_helloqtworld.cpp
RESOURCES += helloqtworld.qrc
Can also someone try to explain for me how Signals and Slots work? Thank you in advance. :)
The problem is in this line:
connect(btnShow,SIGNAL(clicked()),lblMsg,SLOT(updateMessage()));
You are connecting with slot of QLabel instead of slot of your class.
This should be changed to
connect(btnShow,SIGNAL(clicked()),this,SLOT(updateMessage()));
Pay attention at new Qt 5 syntax for signals and slots.
Your label of type QLabel does not have a slot named updateMessage().
Your class Test has.
Change the connect to:
connect(btnShow,SIGNAL(clicked()),this,SLOT(updateMessage()));
This will connect the button's clicked signal to your Test objectsupdateMessage` slot.
Your slot is declared at Test class, not QLabel class, but you are trying to connect to the slot as if it was declared at the QLabel:
connect(btnShow,SIGNAL(clicked()),lblMsg,SLOT(updateMessage()));
You should connecto to this, instead of lblMsg:
connect(btnShow,SIGNAL(clicked()),this,SLOT(updateMessage()));

How to get the member object for SLOT in connect()?

I Started learning Qt (5.5) a couple of days ago, and I recently got stuck on something when working with the connect function, specifically the SLOT parameter. I'm calling a member function from the same class that the connect function is called in, but when the SLOT function is triggered it acts like it's creating a new class object. It worked initially when I kept everything in the same class, but this problem popped up when I tried implementing a hierarchy. I wrote a short program to demonstrate my problem.
Main.cpp
#include <QApplication>
#include "MainWindow.h"
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
MainWindow QtWindow;
QtWindow.show();
return app.exec();
}
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QWidget>
#include <QGridLayout>
#include "TopWidget.h"
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QMainWindow *parent = 0);
private:
QWidget *mainWidget;
QGridLayout *mainLayout;
};
#endif // MAINWINDOW_H
MainWindow.cpp
#include "MainWindow.h"
MainWindow::MainWindow(QMainWindow *parent) : QMainWindow(parent){
mainWidget = new QWidget(this);
mainLayout = new QGridLayout(mainWidget);
setCentralWidget(mainWidget);
TopWidget tWidget(this);
mainLayout->addWidget(tWidget.topWidget, 0, 0);
}
TopWidget.h
#ifndef TOPWIDGET_H
#define TOPWIDGET_H
#include <stdlib.h>
#include <QWidget>
#include <QPushButton>
#include <QGridLayout>
#include <QDebug>
#include <QErrorMessage>
class TopWidget : public QWidget
{
Q_OBJECT
public:
TopWidget(QWidget *parent);
QWidget *topWidget;
private:
QGridLayout *wLayout;
QPushButton *Button;
int memVar1;
int memVar2;
private slots:
void testConnect();
//void SlotWithParams(int a, int b);
};
#endif // TOPWIDGET_H
TopWidget.cpp
#include "TopWidget.h"
TopWidget::TopWidget(QWidget *parent) : QWidget(parent){
topWidget = new QWidget(parent);
wLayout = new QGridLayout(topWidget);
memVar1 = 123;
memVar2 = 321;
Button = new QPushButton("Click Me", topWidget);
connect(Button, &QPushButton::clicked, [=](){ TopWidget::testConnect(); });
}
void TopWidget::testConnect(){
qDebug("Button worked");
if(memVar1 != 123 || memVar2 != 321){
qDebug("Linking failed");
}else{
qDebug("Linking success");
}
}
Since I just started with Qt, I don't have a good feel for what's "proper" Qt code, and what I should avoid, so tips in that direction are also appreciated. The following is the qmake file, if that's important.
CONFIG += c++11
CONFIG += debug
CONFIG += console
QT += widgets
QT += testlib
SOURCES += main.cpp
SOURCES += MainWindow.cpp
SOURCES += TopWidget.cpp
HEADERS += MainWindow.h
HEADERS += TopWidget.h
Release:DESTDIR = bin/Release
Release:OBJECTS_DIR = obj/Release
Release:MOC_DIR = extra/Release
Release:RCC_DIR = extra/Release
Release:UI_DIR = extra/Release
Debug:DESTDIR = bin/Debug
Debug:OBJECTS_DIR = obj/Debug
Debug:MOC_DIR = extra/Debug
Debug:RCC_DIR = extra/Debug
Debug:UI_DIR = extra/Debug
When I run the program in debug mode and press the button, it outputs "Button worked" indicating the link to the function was successful, but then outputs "Linking failed" indicating that a new object was created rather than taking the old one. My knowledge of C++ is patchy, since I only pick up what I need to, and I spent hours yesterday trying to fix this, so forgive me if the fix is something ridiculously easy, but I've mentally exhausted myself over this.
The problem comes from this line:
TopWidget tWidget(this);
You are allocating tWidget on the stack, and it gets destroyed just at the end of the MainWindow constructor.
Replace by:
TopWidget * tWidget = new TopWidget(this);
Also, you should replace your connect line by this one
connect(Button, &QPushButton::clicked, this, &TopWidget::testConnect);
It appears that your slot is called even after the TopWidget is destroyed. Qt normally disconnects connections when sender or receiver are destructed, but it's not able to do that when you connect to a lambda.
And finally, you are doing something weird. What is the purpose of your TopWidget class besides just creating another widget and receiving signals on its slot? You never add the TopWidget to any layout, but just its child. TopWidget is never shown, so it should rather derive from QObject only.

undefined reference to function send

I'm a very beginnner at C++ /Qt programming. I've made this simple dialog box that check the QLineEdit, if the text entered is "bob" should enable the OK button.
I can't get it to compile successfully, it gives me:
dialog.cpp|31|undefined reference to `Dialogmio::send()'
What am I doing wrong?
This is dialog.h:
//dialog.h
#ifndef DIALOG_H_INCLUDED
#define DIALOG_H_INCLUDED
#include <QDialog>
class QPushButton;
class QLineEdit;
class Dialogmio : public QWidget
{
public:
Dialogmio(QWidget *parent =0);
signals:
void send ();
public slots:
void recip(QString &text);
private:
QLineEdit *linedit;
QPushButton *buttonOK;
};
#endif
This is dialog.cpp:
//dialog.cpp
#include <QtGui>
#include "dialog.h"
Dialogmio::Dialogmio(QWidget *parent)
: QWidget(parent)
{
linedit = new QLineEdit();
buttonOK = new QPushButton("OK");
buttonOK->setEnabled(FALSE);
connect( linedit, SIGNAL( textChanged(const QString &) ), this, SLOT( recip(const QString &) ));
connect (this,SIGNAL( send()), this, SLOT( buttonOK->setEnabled(true)) );
QHBoxLayout *layout = new QHBoxLayout();
layout->addWidget(linedit);
layout->addWidget(buttonOK);
setLayout(layout);
}
void Dialogmio::recip(QString &text)
{
QString a = linedit->text();
if (a == "bob"){
emit send(); //here it gives me the error
}
}
This is main.cpp:
#include <QApplication>
#include "dialog.h"
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
Dialogmio *dialog = new Dialogmio;
dialog->show();
return app.exec();
}
I've inserted the Q_OBJECT macro as suggested, now I get one more errors on line 7:
dialog.cpp|7|undefined reference to `vtable for Dialogmio'|
You start by including the Qt file for QDialog, but then go on to inherit from QWidget. While inheriting from QWidget is not a problem, was your intention to actually inherit from QDialog(?), in which case you should define your class this way: -
class Dialogmio : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget* parent);
private slots:
void aSlotFunction();
}
The signals and slots mechanism are C++ extensions that are unique to Qt and in order for a class to use it, the class must include the Q_OBJECT macro, which adds all the necessary functionality. During the build stages, Qt parses the header and creates the code required for the extensions, including run-time type information, the dynamic property system and of-course the signals and slots.
As you state you're using codeblocks as the IDE, if it doesn't automatically run qmake before building, you'll need to do that whenever you add any signals or slots to a class in order for the moc (meta-object-compiler) to see them.
Another thing is that the call to connect signals and slots is wrong: -
connect (this,SIGNAL( send()), this, SLOT( buttonOK->setEnabled(true)) );
The parameter in the SLOT macro takes a slot function, so you need to create a slot and connect it to the send signal: -
connect(this, SIGNAL(send()), this, SLOT(aSlotFunction());
Inside the aSlotFunction, you can then call the set enabled for the button: -
void Dialogmio::aSlotFunction()
{
buttonOK->setEnabled(true);
}
If you're using Qt 5, there's an easier syntax of handling the connection: -
connect(this, &Dialogmio::send, this, &Dialogmio::aSlotFunction);
As this syntax accepts pointers to the functions that will be called, they don't actually have to be declared as slots to work. In addition, you do not provide the arguments, so if they change, you won't have to update the connect calls too.

Getting text from QLineEdit to append to QTextEdit upon QLineEdit returnpressed()

I'm trying to write a simple Qt program which takes text inside a QLineEdit and appends it into a QTextEdit object when the return key is pressed.
Here is the code for my program:
#include <QApplication>
#include <QtGui>
#define WIDTH 640
#define HEIGHT 480
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QTextEdit textArea;
textArea.setReadOnly(true);
QLineEdit lineEdit;
QPushButton quit("Quit");
QObject::connect(&quit, SIGNAL(clicked()), qApp, SLOT(quit()));
QHBoxLayout hLayout;
hLayout.addWidget(&lineEdit);
hLayout.addWidget(&quit);
QVBoxLayout vLayout;
vLayout.addWidget(&textArea);
vLayout.addLayout(&hLayout);
QWidget window;
window.setBaseSize(WIDTH, HEIGHT);
window.setLayout(&vLayout);
window.show();
//This is the line I can not get to work
QObject::connect(&lineEdit, SIGNAL(returnPressed()), &textArea, SLOT(append(lineEdit.text())));
return app.exec();
}
Essentially, the problem is connecting the QLineEdit returnPressed() SIGNAL to the QTextEdit append() SLOT. I am hoping someone can point out what is wrong with my code.
Thank you very much in advance for your time.
When you run your program, you should notice on the console the following Qt error output..
Object::connect: No such slot QTextEdit::append(lineEdit.text()) in ..
You would need to qualify the append reference in your call to connect with the QTextEdit variable name textArea.
But that's not going to help much because you can only specify signal and slot method names and parameter types when calling connect so you can't specify lineEdit.text() in there.
Since the append() slot expects a QString, ideally you would want to connect a signal that includes a QString but there is no such signal for QLineEdits.
You pretty much have to write a slot yourself that you can connect to returnPressed() and call textArea.append(lineEdit.text()) from there. You will need to subclass a QObject of some kind to write a slot which would usually mean subclassing QWidget and putting all of your UI building code in its constructor.
You might also notice that your program crashes when you close it. Since Qt likes to manage the destruction of most QObjects itself, it is usually best to allocate all QObject instances on the heap with new. This isn't technically necessary all the time but it is much easier :)
QObject::connect(&lineEdit, SIGNAL(returnPressed()), &textArea, SLOT(append(lineEdit.text())));
returnPressed() doesn't take any arguments, but append(QString) does take one argument; a QString. Thus, if this would work, you would theoretically call append(""), meaning you wouldn't append anything. Using lineEdit.text() wouldn't work either at this place.
I would recommend you to create a class for the widget:
class Widget : public QWidget
{
public:
Widget(QWidget parent = 0);
//Other public functions
private:
//Private functions and variables
public slots:
void boom();
};
Then you can just use
Widget w(0);
w.show();
in your main function.
void boom() would be called by returnPressed(), and it would take lineEdit.text() and append it to the QTextEdit.
I hope this helps.
here is the code it might be helpful.....
#include "hwidget.h"
Hwidget::Hwidget(QWidget *parent) :
QWidget(parent)
{
}
void Hwidget::mainform_init(void)
{
lineeditp = new QLineEdit;
quitp = new QPushButton("&Exit");
hboxlayoutp = new QHBoxLayout;
hboxlayoutp->addWidget(lineeditp);
hboxlayoutp->addWidget(quitp,0,0);
vboxlayoutp = new QVBoxLayout;
texteditp = new QTextEdit;
texteditp->setReadOnly(true);
vboxlayoutp->addWidget(texteditp,0,0);
vboxlayoutp->addLayout(hboxlayoutp);
QWidget *mywin = new QWidget;
mywin->setLayout(vboxlayoutp);
mywin->setWindowTitle("My Sig and Slot");
mywin->show();
lineeditp->setFocus();
}
void Hwidget::mcopy(void)
{
qDebug() <<"i am your copy slot";
texteditp->setText(lineeditp->text());
lineeditp->clear();
}
#include <QApplication>
#include "hwidget.h"
int main (int argc, char *argv[])
{
QApplication app(argc,argv);
Hwidget *hwin = new Hwidget;
hwin->mainform_init();
hwin->connect(hwin->quitp,SIGNAL(pressed()),
qApp,SLOT(quit()));
hwin->connect(hwin->lineeditp,SIGNAL(returnPressed()),
hwin,SLOT(mcopy()));
return app.exec();
return 0;
}
#ifndef HWIDGET_H
#define HWIDGET_H
#include <QWidget>
#include <QTextEdit>
#include <QLineEdit>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QObject>
#include <QString>
#include <QDebug>
class Hwidget : public QWidget
{
Q_OBJECT
public:
explicit Hwidget(QWidget *parent = 0);
void mainform_init(void);
signals:
public slots:
void mcopy(void);
private:
QHBoxLayout *hboxlayoutp;
QVBoxLayout *vboxlayoutp;
public:
QPushButton *quitp;
QLineEdit *lineeditp;
QTextEdit *texteditp;
};
#endif // HWIDGET_H