0

I have two objects, one will hold the graph, and the other a few buttons. How to use (connect) so that when you press button 1, the inscription is displayed in debag or the schedule is filled with a new one?

For example, I press the button created by the class BtnBox and my graph is displayed. How to use connect()?

main.cpp

#include "mainwindow.h"
#include <QApplication>
#include <QThread>
#include "btnbox.h"
#include "plot.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    BtnBox *panel = new BtnBox(&a);
    Plot *plot = new Plot();

    QObject::connect(panel, SIGNAL(clickedBtn1()), plot, SLOT(slotPrinter()));
//    panel->show();

    QHBoxLayout *mainLayout = new QHBoxLayout;
    mainLayout->addWidget(plot);
    mainLayout->addWidget(panel);

    QWidget window;

    window.setLayout(mainLayout);
    window.show();

    return a.exec();
}
goose
  • 109
  • 1
  • 10
  • Does `BtnBox` actually have a `clickedBtn1()` signal, and `Plot` a `slotPrinter()` slot? – Nikos C. Jun 23 '19 at 11:09
  • Yes, `BtnBox` has a method `clickedBtn1()` as signal, but I don't now know to write there. What I need to write in `clickedBtn1()`? And `slotPrinter()` write in debug console something, for example – goose Jun 23 '19 at 11:36
  • ***What I need to write in clickedBtn1()?*** nothing you don't implement signals. – drescherjm Jun 23 '19 at 12:43

2 Answers2

0

Ok, I assume you have class BtnBox, and it already has this in the class definition (in .h file usually):

signals:
    void clickedBtn1();

Qt moc will generate the implementation of that method, you don't need to do anything more for it here. But you do need to get that signal emitted. In many cases you would add emit clickedBtn1(); in the right places, but in this case you probably want to do something like this in BtnBox::BtnBox constructor:

connect(ui->button1, SIGNAL(clicked()), this, SIGNAL(clickedBtn1()));

Connecting signal to signal will simply do signal forwarding. Replace ui->button1 with the correct pointer to the button, whatever you have in your code.


Note on how to not do it, just to provide a bit of food for thought: Alternative way would be to expose the button 1 from the class, so in your main() you could then do something like this: QObject::connect(panel->getButton1(), SIGNAL(clickedBtn1()), plot, SLOT(slotPrinter()));. But this is generally considered a bit dirty, exposing internals of BtnBox class like that. It's better to expose the signal, and then the code using the class does not need to care how it gets emitted (for example from several different parts of BtnBox), or how internal implementation of BtnBox might change (for example converting it to QML).

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
hyde
  • 60,639
  • 21
  • 115
  • 176
0

Maybe you can use the QPushButton::clicked signal and write something like this:

connect(ui->pushButtonObj, &QPushButton::clicked, plot, &Plot::slotPrinter);

But if you want a custom behavior with your class BtnBox you can create on header file of BtnBox a signal.

signals:
    void clickedBtn1();

And use: emit clickedBtn1(); whenever you want to emit it, your connect should work.

There is no need for implementation of the signal, you just have to emit it.

The emit keyword is not really necessary, if you want you can simply use clickedBtn();

  • Yes, it's work. More I want use another method connect(). For this, I had to make the slot `slotPointer()` public and the first button. But I'm trying to leave them in private. The class `Plot` and `BtnBox` contain all the necessary graphic elements, i.e. buttons and timeline. I can’t access the buttons from the class `BtnBox` and the slot `slotPrinter` as they are private. How can I keep these properties private, but use a different type of connect? plot.h ` private slots: void slotPrinter(); ` btnbox.h ` private: QPushButton *btn1; ` – goose Jun 23 '19 at 17:21
  • Consider do the connection inside the BtnBox, it might work because you will have access to all buttons. I think that slotPrinter can be connected even though it's private, because the public and private should be only applied if you call slotPrinter without a signal (as a function). – Willian Abreu Ferreira Jun 23 '19 at 17:56
  • You can do the connection like this: `connect(ui->pushButton1,SIGNAL(clicked()),plot, SLOT(slotPrint()));` but in this case you will need the plot object inside the BtnBox. – Willian Abreu Ferreira Jun 23 '19 at 18:21