ui detect mouse event on objects
-
// you can use toggled() or clicked() connect(cb, &QCheckBox::toggled, cb, [id, i](bool checked)) { // here you have 'i'
what exactly happend here?
you connect the checkbox toggled/clicked event to ???
Where can I catch this event for further use? -
// you can use toggled() or clicked() connect(cb, &QCheckBox::toggled, cb, [id, i](bool checked)) { // here you have 'i'
what exactly happend here?
you connect the checkbox toggled/clicked event to ???
Where can I catch this event for further use?@Pfanne
It's a C++ lambda, like an "anonymous function". Search forlambda
on https://wiki.qt.io/New_Signal_Slot_Syntax. @mpergand uses it here to pass thei
value from thefor
loop for you to use in the lambda code, which identifies which checkbox has been toggled. -
@Pfanne
You may have a look at QButtonGroup or QGroupBoxI take a look at QButtonGroup, this look like the right object for my usecase.
I tryed this:*.h
private slots: void on_ioGroup_clicked(int id);
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QButtonGroup* ioGroup = new QButtonGroup(this); ioGroup->addButton(ui->IO_1, 1; ioGroup->addButton(ui->IO_2, 2); connect(ioGroup, SIGNAL(buttonClicked(int)), this, SLOT(on_ioGroup_clicked(int))); } ---- void MainWindow::on_ioGroup_clicked(int id) { qDebug() << "id clicked" << id; } ``` but "on_ioGroup-clicked" will not be executed by clicking on IO_1 or IO_2.
-
I take a look at QButtonGroup, this look like the right object for my usecase.
I tryed this:*.h
private slots: void on_ioGroup_clicked(int id);
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QButtonGroup* ioGroup = new QButtonGroup(this); ioGroup->addButton(ui->IO_1, 1; ioGroup->addButton(ui->IO_2, 2); connect(ioGroup, SIGNAL(buttonClicked(int)), this, SLOT(on_ioGroup_clicked(int))); } ---- void MainWindow::on_ioGroup_clicked(int id) { qDebug() << "id clicked" << id; } ``` but "on_ioGroup-clicked" will not be executed by clicking on IO_1 or IO_2.
-
@Pfanne
Use the "modern" syntax for yourconnect()
s and you would see there is nobuttonClicked(int)
withint
parameter signal. OTOH there is aQButtonGroup::idClicked(int id)
signal.yes, I also saw that "buttonClicked(int)" is not up to date!
this worked:
connect(ioGroup, &QButtonGroup::idClicked, this, MainWindow::on_ioGroup_clicked);
this not:
connect(ioGroup, &QButtonGroup::idClicked(int id), this, MainWindow::on_ioGroup_clicked(int id));
-
yes, I also saw that "buttonClicked(int)" is not up to date!
this worked:
connect(ioGroup, &QButtonGroup::idClicked, this, MainWindow::on_ioGroup_clicked);
this not:
connect(ioGroup, &QButtonGroup::idClicked(int id), this, MainWindow::on_ioGroup_clicked(int id));
finaly this worked:
connect(ioGroup, SIGNAL(idClicked(int)), this, SLOT(on_ioGroup_clicked(int)));
void MainWindow::on_ioGroup_clicked(int id) { qDebug() << "id clicked" << id; QAbstractButton* cb = ioGroup->button(id); qDebug() << "Object" << cb->objectName(); }
-
yes, I also saw that "buttonClicked(int)" is not up to date!
this worked:
connect(ioGroup, &QButtonGroup::idClicked, this, MainWindow::on_ioGroup_clicked);
this not:
connect(ioGroup, &QButtonGroup::idClicked(int id), this, MainWindow::on_ioGroup_clicked(int id));
@Pfanne said in ui detect mouse event on objects:
this worked:
connect(ioGroup, &QButtonGroup::idClicked, this, MainWindow::on_ioGroup_clicked);
Correct.
finaly this worked:
connect(ioGroup, SIGNAL(idClicked(int)), this, SLOT(on_ioGroup_clicked(int)));
Since you had it correct first why change it?
-
yes, I also saw that "buttonClicked(int)" is not up to date!
this worked:
connect(ioGroup, &QButtonGroup::idClicked, this, MainWindow::on_ioGroup_clicked);
this not:
connect(ioGroup, &QButtonGroup::idClicked(int id), this, MainWindow::on_ioGroup_clicked(int id));
-
@Pfanne said in ui detect mouse event on objects:
connect(ioGroup, &QButtonGroup::idClicked, this, MainWindow::on_ioGroup_clicked);
You must specify an adress (&) here:
&MainWindow::on_ioGroup_clicked); -
@mpergand said in ui detect mouse event on objects:
&MainWindow::on_ioGroup_clicked);
OIC I missed that when OP said it was correct.
-
@mpergand said in ui detect mouse event on objects:
How to set text color as you do in red in your posts ?
Interesting. I don't know and have never set a text color in any of my posts! :) @Pl45m4 post above with red text for
Text
is the first time I have seen any color.Oh, hang on! Like
buttonClicked()
? LMAO, until today's forum update that was in green, no wonder I didn't know about red! The "color" is just up to the forum style. The important thing is that code is inmonospace
font. Use the Code icon (</>
) when you are typing in. Or`in-line codecharacters`
```
multiline code multiline code
```
First one is single-backtick inline, second one is triple-backtick above and below block. -
J JonB referenced this topic on
-
@mpergand said in ui detect mouse event on objects:
How to set text color as you do in red in your posts ?
Interesting. I don't know and have never set a text color in any of my posts! :) @Pl45m4 post above with red text for
Text
is the first time I have seen any color.Oh, hang on! Like
buttonClicked()
? LMAO, until today's forum update that was in green, no wonder I didn't know about red! The "color" is just up to the forum style. The important thing is that code is inmonospace
font. Use the Code icon (</>
) when you are typing in. Or`in-line codecharacters`
```
multiline code multiline code
```
First one is single-backtick inline, second one is triple-backtick above and below block.@JonB said in ui detect mouse event on objects:
Interesting. I don't know and have never set a text color in any of my posts! :) @Pl45m4 post above with red text for Text is the first time I have seen any color.
I use this text format every time I mention some Qt term or class.
QPushButton
,QObject
,Q_OBJECT
.
So did/do you ;-)
I don't know what exactly you did when writing these posts, but I used single backticks (don't know how they are called)
` QPushButton ` (now escaped to make them visible)LMAO, until today's forum update that was in green, no wonder I didn't know about red
I could swear it was red before, with some kind of red-ish background.
At least when used outside of a code section in regular paragraphs. -
@JonB said in ui detect mouse event on objects:
Interesting. I don't know and have never set a text color in any of my posts! :) @Pl45m4 post above with red text for Text is the first time I have seen any color.
I use this text format every time I mention some Qt term or class.
QPushButton
,QObject
,Q_OBJECT
.
So did/do you ;-)
I don't know what exactly you did when writing these posts, but I used single backticks (don't know how they are called)
` QPushButton ` (now escaped to make them visible)LMAO, until today's forum update that was in green, no wonder I didn't know about red
I could swear it was red before, with some kind of red-ish background.
At least when used outside of a code section in regular paragraphs. -
@mpergand said in ui detect mouse event on objects:
How to set text color as you do in red in your posts ?
` Text ` =
Text
-
@Pfanne said in ui detect mouse event on objects:
this worked:
connect(ioGroup, &QButtonGroup::idClicked, this, MainWindow::on_ioGroup_clicked);
Correct.
finaly this worked:
connect(ioGroup, SIGNAL(idClicked(int)), this, SLOT(on_ioGroup_clicked(int)));
Since you had it correct first why change it?
-
@JonB
the first version:connect(ioGroup, &QButtonGroup::idClicked, this, MainWindow::on_ioGroup_clicked);
is without the object ID, implementing the ID in this version a error occurse.
@Pfanne said in ui detect mouse event on objects:
connect(ioGroup, &QButtonGroup::idClicked, this, MainWindow::on_ioGroup_clicked);
As I said earlier, you have to pass an address:
connect(ioGroup, &QButtonGroup::idClicked, this,
&
MainWindow::on_ioGroup_clicked); -
@mpergand said in ui detect mouse event on objects:
&MainWindow::on_ioGroup_clicked);
OIC I missed that when OP said it was correct.
@JonB said in ui detect mouse event on objects:
@mpergand said in ui detect mouse event on objects:
&MainWindow::on_ioGroup_clicked);
OIC I missed that when OP said it was correct.
You can see for yourself (when it's written in monospace) that in your
connect(ioGroup, &QButtonGroup::idClicked, this, MainWindow::on_ioGroup_clicked);
one method reference has a preceding
&
and the other does not, and thereby tell what you had is not correct. -
@JonB said in ui detect mouse event on objects:
@mpergand said in ui detect mouse event on objects:
&MainWindow::on_ioGroup_clicked);
OIC I missed that when OP said it was correct.
You can see for yourself (when it's written in monospace) that in your
connect(ioGroup, &QButtonGroup::idClicked, this, MainWindow::on_ioGroup_clicked);
one method reference has a preceding
&
and the other does not, and thereby tell what you had is not correct.connect(ioGroup, &QButtonGroup::idClicked, this, MainWindow::on_ioGroup_clicked);
OK, I think I understand, but this try worked, without ID but it creates a event on every click.
Without the reference (&) to MainWindow....But it´s OK, this try:
connect(ioGroup, SIGNAL(idClicked(int)), this, SLOT(on_ioGroup_clicked(int)));
is exactly the way that I use (SIGNAL -> SLOT) for my other connections.
Thanks for your support!
-
connect(ioGroup, &QButtonGroup::idClicked, this, MainWindow::on_ioGroup_clicked);
OK, I think I understand, but this try worked, without ID but it creates a event on every click.
Without the reference (&) to MainWindow....But it´s OK, this try:
connect(ioGroup, SIGNAL(idClicked(int)), this, SLOT(on_ioGroup_clicked(int)));
is exactly the way that I use (SIGNAL -> SLOT) for my other connections.
Thanks for your support!
@Pfanne said in ui detect mouse event on objects:
is exactly the way that I use (SIGNAL -> SLOT) for my other connections.
But you should switch to the pmf syntax for your own sake.