I was trying to figure out how this code will loke like in plain C++ without any dependency so I was using the moc compiler but apparently I'm wrong.
moc always returns
main.cpp:0: Note: No relevant classes found. No output generated.
the code is
#include <QApplication>
#include <QWidget>
#include <QPushButton>
class MyButton : public QWidget
{
public:
MyButton(QWidget *parent = 0);
};
MyButton::MyButton(QWidget *parent)
: QWidget(parent)
{
QPushButton *quit = new QPushButton("Quit", this);
quit->setGeometry(50, 40, 75, 30);
connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyButton window;
window.resize(250, 150);
window.move(300, 300);
window.setWindowTitle("button");
window.show();
return app.exec();
}
from http://www.zetcode.com/gui/qt4/firstprograms/
In general terms I'm interested in creating my own signal slot system using only the C++ standard library ( no boost signal, no QT, no nothing else ) so I'm doing this for research purpose and I'm only interested on the infrastructure about signals and slots.
Thanks.
Add the Q_OBJECT macro to the private section of your class, so moc converts it.
class MyButton : public QWidget
{
Q_OBJECT
public:
MyButton(QWidget *parent = 0);
};
Here is what the documentation says.
Related
I'm trying to create a class for signal/slot connection (old syntax, Qt 4.8) and I am doing something wrong as I keep receiving a template error: invalid declaration of member template in local class... That has obviously something to do with the Q_OBJECT macro... What should I do? Here is a modeled program:
#include <QtGui>
#include <QtCore>
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();
class MyObject : public QObject
{
Q_OBJECT /* the problem is somewhere here... */
public:
QTextEdit text;
QString a;
public slots:
void onClicked() {
text.setText(a);
}
};
QPushButton btn ("Convert", &mw);
QObject::connect(
&btn,
SIGNAL(clicked()),
this,
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();
}
Qt's MOC can process neither nested classes nor local classes. You will have to move the class definition outside main. The documentation only mentions nested classes, but the limitation does apply to local classes too.
Update:
If I delete Q_OBJECT, and I do not use SLOT SINGAL, just use connect() like this:
connect(this, &QWidget::destroyed, this, &QWidget::myslot),
my code will run well without any warnings and errors.
I want to write a little code to instruct some classes, so I try to simplify my code. But I encountered some strange things. I cannot write a simple widget in my main.cpp. If I write the widget in mywidget.cpp and mywidget.h, the program runs well. If I want to write the widget in the main.cpp, what should I do?
This is my code.
#include <QApplication>
#include <QWidget>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
};
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
}
Widget::~Widget()
{
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
And the error information is:
Add following line to the end of main.cpp and rerun qmake:
#include "main.moc"
That will invoke moc tool for your main.cpp. It generates meta-object function definitions for your Widget class resolving your linker errors when you rebuild.
As stated in documentation:
Whenever qmake is run, it parses the project's header files and
generates make rules to invoke moc for those files that contain a
Q_OBJECT macro.
Thus, put the class declaration in a header file (e.g. widget.h) and the class definition in a source file with the same name (e.g. widget.cpp).
If you still want to make it work with signals you can remove the Q_OBJECT macro and use the QObject::connect()
for example:
#include <QApplication>
#include <QWidget>
#include <iostream>
class Widget : public QWidget
{
//Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
};
Widget::Widget(QWidget *parent) : QWidget(parent)
{
// say bey
QObject::connect(this,&Widget::destroyed,
[](){std::cout<<"bye"<<std::endl;});
}
Widget::~Widget()
{
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
I test it on Qt5.9.4
I made class inherited from QLabel. This class also have public slot, that should change label caption. I "call" this SLOT with clicked() SIGNAL of button.
So nothing happened when I press the button.
#include <QApplication>
#include <QLabel>
#include <QPushButton>
class Label : public QLabel
{
public:
Label(QString a) : QLabel(a){}
public slots:
void change()
{
this->setNum(2);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPushButton* button = new QPushButton("Button");
Label* lbl = new Label("Label");
button->show();
lbl->show();
QObject::connect(button, SIGNAL(clicked(bool)), lbl, SLOT(change()));
return a.exec();
}
What should I do to change caption from slot?
In order for the signals and slots to be recognized, the classes must use the Q_OBJECT macro in the private part.
Another thing to do is to include "main.moc", for more information on this point read this.
#include <QApplication>
#include <QLabel>
#include <QPushButton>
class Label : public QLabel
{
Q_OBJECT
public:
Label(const QString &text, QWidget *parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags()) :
QLabel(text, parent, f){}
public slots:
void change()
{
setNum(2);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPushButton* button = new QPushButton("Button");
Label* lbl = new Label("Label");
button->show();
lbl->show();
QObject::connect(button, SIGNAL(clicked()), lbl, SLOT(change()));
return a.exec();
}
#include "main.moc"
At the end of making these changes you must execute the following:
Press clean all in the Build menu.
then run qmake in the same menu.
And you just compose your project.
Add Q_OBJECT after
class Label : public QLabel
{
and then you should
either place your Label class declaration to a .h file or write #include "main.moc" after main function declaration.
try to get the return value from your connect call an check it for true or false.
Add Q_OBJECT Macro to the beginning of your derived class.
Add some debug output to your slot like
qDebug()<<"This is my slot.";
Maybe this would help to get a little further.
Best regards
I'm writing a QT project in Xcode, I made a Widget application in the QT Editor and used the "qmake -spec macx-xcode" to convert the project into an Xcode project.
I have a standard project:
main.cpp
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow w;
w.show();
return app.exec();
}
main window.cpp
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
m_button = new QPushButton(this);
m_button -> setText("button");
m_button->setGeometry(QRect(QPoint(100, 100),QSize(200, 50)));
QPushButton *workingButton = new QPushButton("Hello");
workingButton -> show();
connect(m_button, SIGNAL(clicked()), this, SLOT(quitButton()));
ui->setupUi(this);
}
void MainWindow::quitButton() {
m_button->setText("Example");
}
MainWindow::~MainWindow()
{
delete ui;
}
main window.h
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void quitButton();
private:
Ui::MainWindow *ui;
QPushButton *m_button;
};
#endif
The m_button shows up in the mainWindow but it is not clickable but the workingButton, shows up in its own separate window, and in the connect, when I replace the m_button with the workingButton, it is able to call the function. Any idea why the m_button is not sending a signal or function not being called?
The reason is quite simple: you have other transparent widgets overlaid on top of m_button. You must ensure that the button is not covered by anything else. E.g. move the creation of the button after the setupUi call or make the button a child of the central widget. Generally speaking, the setupUi call should be the first thing in a widget's constructor.
You also don't need to dynamically allocate the child widgets: prefer holding things by value: less things can go wrong then, and you're having less overhead, too!
Thus, pretending that the Ui_MainWindow class was really generated by uic:
// https://github.com/KubaO/stackoverflown/tree/master/questions/simple-button-main-41729401
#include <QtWidgets>
class Ui_MainWindow {
public:
QWidget *central;
QGridLayout *layout;
QLabel *label;
void setupUi(QMainWindow *parent);
};
class MainWindow : public QMainWindow, private Ui_MainWindow {
Q_OBJECT
QPushButton m_button{"Click Me"};
public:
MainWindow(QWidget *parent = nullptr) : QMainWindow(parent) {
setupUi(this);
m_button.setParent(centralWidget());
m_button.setGeometry({{50, 50}, m_button.sizeHint()});
}
};
void Ui_MainWindow::setupUi(QMainWindow *parent) {
central = new QWidget{parent};
layout = new QGridLayout{central};
label = new QLabel{"Hello"};
label->setAlignment(Qt::AlignCenter);
label->setStyleSheet("background-color:blue; color:white;");
layout->addWidget(label, 0, 0);
parent->setCentralWidget(central);
parent->setMinimumSize(200, 200);
}
int main(int argc, char ** argv) {
QApplication app{argc, argv};
MainWindow w;
w.show();
return app.exec();
}
#include "main.moc"
#include <QApplication>
#include <QFont>
#include <QPushButton>
#include <QWidget>
class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0);
};
MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
setFixedSize(200, 120);
QPushButton *quit = new QPushButton(tr("Quit"), this);
quit->setGeometry(62, 40, 75, 30);
quit->setFont(QFont("Times", 18, QFont::Bold));
connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;
widget.show();
return app.exec();
}
For this line : MyWidget(QWidget *parent = 0);
why we need to put = 0 here??
It is called an Default parameter
Basically you are saying unless you pass another value, the function (or constructor in this case) will be called with parent as 0.
When you'd had MyWidget(QWidget *parent); as constructor, you'd had to call it like MyWidget widget(0);
You do not have to put zero there. C++ allows you to put default value for a parameter. In this case, parameter parent will default to 0 if the constructor is invoked without specifying an argument.
Don't need to put it there, but it's a default value. If you don't pass any value to the constructor, it will take the '0' as value. It's making things a bit easier in some cases for the programmer.