Pushbutton crashed.
-
@Gutks said in Pushbutton crashed.:
Not being able to access the emitPressed() and emitRelease(), class scope QAbstractButton.
Why not? Your own pushbutton class inherits
QPushbutton
and you have added some signals on your own... Just emit these.
(emit pressed()
is the wrong signal after all. You named you signals in your button class differenta and these are the ones, you need to use inside your mouse event)And again my question :)
Where do you create (new
) your button instancesbutton1
andbutton2
?
ui->button1
only works, if you've created your button with QtDesigner (or if they are part of yourui_classname.h
at least).Edit:
Do you make use of widget promotion? If yes, it may work like this, but if you don't, you'll need to instantiate your buttons first, in order to connect something to them.
@Gutks said in Pushbutton crashed.:
QObject::connect: No such signal QPushButton::leftClicked()
This indicates, that your
ui->button1
is currently not your custom button. I would say, these are still pointers to your basicQPushButtons
.
Read about "Promoting Widgets in Qt" (Link) or create yourCustomPushbuttons
by code and add them to you layout manually.Your
QAbstractButton *p
inside your mouseEvents also does nothing and is not necessary. What do you expect from doing this? :)@Gutks said in Pushbutton crashed.:
I would have work to get the value of the line edit and convert string to number, and so do the increment
and decrement.No, you dont. Only if you want to access these values from "outside". If you use them only their own class, you dont need getter / setter. In addition, you could make use of signals & slots to change the values, but if these are just counters, you dont even need to do that.
-
NameProjet= Test;
classe1= customPushButton.h ; customPushButton;
Classe2= test.h ; test.cpp = ( Widget, ui->classe)
other= main.cpp;I tried to use this logic similar to the previous tutorial, however, Macro ( Q_Q() ),
it is not stated, when I try to instantiate q->emitPressed(). return error: use of undeclared identifier 'q_func';I tried to change the 'Q' for 'D' ( Q_D), equal to the situation below, returns another.
error: cannot initialize a variable of type 'QAbstractButtonPrivate *const' with an rvalue of type 'QPushButtonPrivate'
Q_D(Class)void QAbstractButton::mousePressEvent(QMouseEvent *e){ Q_D(QAbstractButton); if (e->button() != Qt::LeftButton) { e->ignore(); return; } if (hitButton(e->pos())) { setDown(true); d->pressed = true; repaint(); d->emitPressed(); e->accept(); } else { e->ignore(); } }
Ok, I'm going to make the right sign and study the custom widgets with Qt Designer. Thank you for the patience, using google translation . I return with possible results or errors.
-
-
@Gutks said in Pushbutton crashed.:
instantiate: emitPressed(); emitRelease();
Mmm, strange that you need to "instantiate" what seems to be signals...
-
@Gutks
Yes, but why are you doing so, given thatemitPressed()
is not documented and private to the innards ofQAbstractButton
? Which is why you are running into the compilation errors.... My question is: why are trying to do anything like this? The fact that it's inQAbstractButtonPrivate
should give you a clue. -
The implementation of this function is accessing Q_D, since I am re-implementing it, I thought I could do the same.
void QAbstractButton::mousePressEvent(QMouseEvent *e)
I will study the qt designer, to try to solve, otherwise, I will create an Auxiliary button to remove in the edit line. I think it's simpler than using events
-
@Gutks
But why are you "re-implementing" it at all? Usually a user class sub-classes, not re-implements. Re-implementing is fraught, e.g. what happens if the internals change over time. Since you say "I'm a beginner" I cannot see why you would want to do such a thing, when 99.9% of other users do not.I keep saying the same thing, so I'll leave it to you now.
-
@Gutks said in Pushbutton crashed.:
if(p->button()==Qt::LeftButton){ui->lineEdit1->setText(QString::number(value1++));} else{if(p->button()==Qt::RightButton){ui->lineEdit1->setText(QString::number(value1--));}
From your requirements it's apparent you need a custom QPushButton that extends existing one by adding just the rightclicked() signal, and nothing else. So you'll have both signals (existing left click) and new one (right click) to connect as you wish.
Please take a look at this SO question & answers.
-
@Gutks
If what @Pablo-J-Rogina has discerned is correct --- you just want a left-/right-click to increment/decrement a number in aQLineEdit
, is that right? --- then you would be better just using aQSpinBox
, which is for entering numbers, and you wouldn't have any of this..... -
@JonB said in Pushbutton crashed.:
then you would be better just using a QSpinBox,
I think the goal here is the rightclicked() signal, what you connect that to is not relevant at this point
-
@Pablo-J-Rogina
Oh, I see. You may be right. Or it just might turn out the user was going to use a left-/-right-click on a button to inc/dec a number, you never know... :)On a different tone, it's "unusual" to have the user right-click on a button. Possible, but possibly not intuitive. A "rocker" pushbutton (click at left/right for different behaviour, but with left mouse) might be another possibility.
-
Button.h
#ifndef LR_CLICKBUTTON_H #define LR_CLICKBUTTON_H #include <QPushButton> class LR_ClickButton: public QPushButton { Q_OBJECT public: LR_ClickButton(QWidget *parent = nullptr); protected: void mousePressEvent(QMouseEvent *e) override; void mouseReleaseEvent(QMouseEvent *e) override; signals: // Not needed, since one signal which sends MouseEvent to get handled in slot //void leftClicked(); //void rightClicked(); void myClicked(QMouseEvent *e); }; #endif // LR_CLICKBUTTON_H
Button.cpp
#include "lr_clickbutton.h" #include <QMouseEvent> #include <QDebug> LR_ClickButton::LR_ClickButton(QWidget *parent): QPushButton(parent) { } void LR_ClickButton::mousePressEvent(QMouseEvent *e) { QPushButton::mousePressEvent(e); } void LR_ClickButton::mouseReleaseEvent(QMouseEvent *e) { if(e->button() == Qt::LeftButton) { qDebug() << "Left"; emit myClicked(e); } else if (e->button() == Qt::RightButton) { qDebug() << "Right"; emit myClicked(e); } QPushButton::mouseReleaseEvent(e); }
MainWindow.h
public slots: void myButtonClicked(QMouseEvent *e);
MainWindow.cpp
// ######### C'TOR ##################### LR_ClickButton *button1 = new LR_ClickButton(this); centralWidget()->layout()->addWidget(button1); connect(button1, &LR_ClickButton::myClicked, this, &MainWindow::myButtonClicked); // ###################################### void MainWindow::myButtonClicked(QMouseEvent *e) { if(e->button() == Qt::LeftButton) { // Of course you can put anything you want here int val = ui->spinBox->value(); ui->spinBox->setValue(val + 1); } else if (e->button() == Qt::RightButton) { // or here int val = ui->spinBox->value(); ui->spinBox->setValue(val - 1); } }
Wasn't that hard, right? :)
Result:
3 left-clicks increase value by 3.
2 right-clicks decrease value by 2 again.
-
@JonB said in Pushbutton crashed.:
On a different tone, it's "unusual" to have the user right-click on a button. Possible, but possibly not intuitive
Might not be the most intuitive solution but this is actually used a lot in games (with a hint to this behavior ofc)
-
@Christian-Ehrlicher @Pl45m4 @JonB @Pablo-J-Rogina
Thank you very much, you don't know how it helped me. Otherwise, I would have to create an internal storage system for each button between the tabs and remove it when editing the line.
Yes, and a tool I am creating to help the 1 game community. How does it activate as resolved?