Solved How to change the QToolButton opacity when the cursor is on int?
-
AHHHHHH.
No :)
This (
bool eventFilter(QObject *obj, QEvent *event) override;
) is not the implementation.HEADER (
Dialog.h
)protected: bool eventFilter(QObject *obj, QEvent *event) override;
DIALOG.CPP
bool Dialog::eventFilter(QObject *obj, QEvent *event) { if(obj == YOUR_BUTTON) { if(event->type() == QEvent::Enter) { // SET YOUR OPACITY } else if(event->type() == QEvent::Leave) { // SET OPACITY BACK TO NORMAL } return true; } else { return QDialog::eventFilter(obj, event); }
-
@Pl45m4 Really sorry :( I set that part to my cpp file :
QImage image; image.load("F:/qt codes/opacity4/2-2"); image = image.convertToFormat(QImage::Format_ARGB32); QPainter painter(&image); painter.setOpacity(0.5); QPixmap pixmap = QPixmap::fromImage(image); QIcon icon(pixmap); butt->setIcon(icon); butt->setIconSize(QSize(widthW/2,heightW/3.3)); butt->setText("my book"); butt->setToolButtonStyle(Qt::ToolButtonTextUnderIcon); butt->setCursor(Qt::PointingHandCursor); effect = new QGraphicsOpacityEffect(this); effect->setOpacity(0.5); butt->setGraphicsEffect(effect); //butt->setAttribute(Qt::WA_Hover, true); butt->setMouseTracking(true); butt->installEventFilter(this); } Dialog::~Dialog() { delete ui; } bool Dialog::eventFilter(QObject *obj, QEvent *event) { if(obj == butt) { if(event->type() == QEvent::Enter) { // SET YOUR OPACITY effect->setOpacity(1); butt->setGraphicsEffect(effect); } else if(event->type() == QEvent::Leave) { // SET OPACITY BACK TO NORMAL effect->setOpacity(0.5); butt->setGraphicsEffect(effect); } return true; } else { return QDialog::eventFilter(obj, event); } } /*void Dialog::enterEvent(QEvent * event) { QToolButton::enterEvent(event); effect->setOpacity(1); butt->setGraphicsEffect(effect); }*/
But got this error:
-
I assume you still have the
Dialog::eventFilter
implementation in your header file?!
(Because it says, that you are re-defining it in your cpp).If not, try a "Clean" followed by "Build" or "Rebuild" and "Run QMake".
-
@Pl45m4 Oh sorry. I have thought you mean the protected part of the header file.
My header file now is:#ifndef DIALOG_H #define DIALOG_H #include <QDialog> #include <QToolButton> #include <QGraphicsOpacityEffect> #include <QEvent> namespace Ui { class Dialog; } class Dialog : public QDialog { Q_OBJECT public: explicit Dialog(QWidget *parent = 0); ~Dialog(); QToolButton *butt; QGraphicsOpacityEffect *effect; private: Ui::Dialog *ui; protected: bool eventFilter(QObject *obj, QEvent *event) override; }; #endif // DIALOG_H
and my cpp file is:
#include "dialog.h" #include "ui_dialog.h" #include <QDesktopWidget> #include <QIcon> #include <QPixmap> #include <QToolButton> #include <QImage> #include <QPainter> #include <QGraphicsOpacityEffect> #include <QDebug> Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); QDesktopWidget wid; int screenWidth = wid.screen()->width(); int screenHeight = wid.screen()->height(); this->setFixedSize(screenWidth/2.5,screenHeight/1.5); int widthW = this->frameGeometry().width(); int heightW = this->frameGeometry().height(); //qDebug() << widthW; //qDebug() << heightW; this->setGeometry((screenWidth/2)-(widthW/2),(screenHeight/2)-(heightW/2),widthW,heightW); butt= new QToolButton(this); butt->setFixedSize(widthW/2.5,heightW/2.5); int width_button_EM = butt->frameGeometry().width(); int height_buttin_EM = butt->frameGeometry().height(); butt->setGeometry(widthW*50/100,heightW*20/100,width_button_EM,height_buttin_EM); butt->setObjectName("butt-name"); butt->setStyleSheet( "QToolButton#butt-name {" "border:5px solid #303030;" "border-width: 3px;" " }" " QToolButton#butt-name {" " border-radius: 0px;" " }" "QToolButton#butt-name {" "padding-left:5px; padding-right:5px; padding-bottom:5px; padding-top:5px;" "}" ); QImage image; image.load("F:/qt codes/opacity4/2-2"); image = image.convertToFormat(QImage::Format_ARGB32); QPainter painter(&image); painter.setOpacity(0.5); QPixmap pixmap = QPixmap::fromImage(image); QIcon icon(pixmap); butt->setIcon(icon); butt->setIconSize(QSize(widthW/2,heightW/3.3)); butt->setText("my book"); butt->setToolButtonStyle(Qt::ToolButtonTextUnderIcon); butt->setCursor(Qt::PointingHandCursor); effect = new QGraphicsOpacityEffect(this); // effect->setOpacity(0.5); //butt->setGraphicsEffect(effect); //butt->setAttribute(Qt::WA_Hover, true); butt->setMouseTracking(true); butt->installEventFilter(this); } Dialog::~Dialog() { delete ui; } bool Dialog::eventFilter(QObject *obj, QEvent *event) { if(obj == butt) { if(event->type() == QEvent::Enter) { // SET YOUR OPACITY effect->setOpacity(1); butt->setGraphicsEffect(effect); } else if(event->type() == QEvent::Leave) { // SET OPACITY BACK TO NORMAL effect->setOpacity(0.5); butt->setGraphicsEffect(effect); } return true; } else { return QDialog::eventFilter(obj, event); } }
But the output is only an empty dialog window without button and button icon.
-
How is your button set to your dialog and where?
QDesktopWidget
is deprecated and shouldn't be used anymore. All in all, you picked the wrong approach. Better apply someQLayout
(horizontal or vertical) to yourQDialog
and then add your button. -
@Pl45m4 Actually I have used QDesktopWidget, because I want to set the geometry based on my screen width and height.
When I comment the part:
bool Dialog::eventFilter(QObject *obj, QEvent *event) { if(obj == butt) { if(event->type() == QEvent::Enter) { effect->setOpacity(1); butt->setGraphicsEffect(effect); } else if(event->type() == QEvent::Leave) { effect->setOpacity(0.5); butt->setGraphicsEffect(effect); } return true; } else { return QDialog::eventFilter(obj, event); } }
from my cpp file , and the part:
protected: bool eventFilter(QObject *obj, QEvent *event) override;
from my header file, I got the following output:
And when I don't comment those parts, I got an empty dialog window.
-
@nanor said in How to change the QToolButton opacity when the cursor is on int?:
I want to set the geometry based on my screen width and height
QScreen
is the alternative / replacement.
https://doc.qt.io/qt-5/qscreen.htmlYour button is floating around / just drawn on your widget (dialog).
It is always better to use layouts unless there is any reason to not, which I dont see in your case :)
dialog.cpp
(afterbutt->setCursor(Qt::PointingHandCursor);
)QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(butt); setLayout(layout);
-
@Pl45m4 I added the layout and now my cpp file is:
#include "dialog.h" #include "ui_dialog.h" #include <QDesktopWidget> #include <QIcon> #include <QPixmap> #include <QToolButton> #include <QImage> #include <QPainter> #include <QGraphicsOpacityEffect> #include <QDebug> #include <QVBoxLayout> Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); /*QDesktopWidget wid; int screenWidth = wid.screen()->width(); int screenHeight = wid.screen()->height(); this->setFixedSize(screenWidth/2.5,screenHeight/1.5); int widthW = this->frameGeometry().width(); int heightW = this->frameGeometry().height(); qDebug() << widthW; qDebug() << heightW; this->setGeometry((screenWidth/2)-(widthW/2),(screenHeight/2)-(heightW/2),widthW,heightW); butt->setFixedSize(widthW/2.5,heightW/2.5); int width_button_EM = butt->frameGeometry().width(); int height_buttin_EM = butt->frameGeometry().height(); butt->setGeometry(widthW*50/100,heightW*20/100,width_button_EM,height_buttin_EM);*/ butt= new QToolButton(this); butt->setObjectName("butt-name"); butt->setStyleSheet( "QToolButton#butt-name {" "border:5px solid #303030;" "border-width: 3px;" " }" " QToolButton#butt-name {" " border-radius: 0px;" " }" "QToolButton#butt-name {" "padding-left:5px; padding-right:5px; padding-bottom:5px; padding-top:5px;" "}" ); QImage image; image.load("F:/qt codes/opacity4/2-2"); image = image.convertToFormat(QImage::Format_ARGB32); QPainter painter(&image); painter.setOpacity(0.5); QPixmap pixmap = QPixmap::fromImage(image); QIcon icon(pixmap); butt->setIcon(icon); // butt->setIconSize(QSize(widthW/2,heightW/3.3)); butt->setIconSize(QSize(100,200)); butt->setText("my book"); butt->setToolButtonStyle(Qt::ToolButtonTextUnderIcon); butt->setCursor(Qt::PointingHandCursor); effect = new QGraphicsOpacityEffect(this); effect->setOpacity(0.5); butt->setGraphicsEffect(effect); //butt->setAttribute(Qt::WA_Hover, true); butt->setMouseTracking(true); butt->installEventFilter(this); QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(butt); setLayout(layout); } Dialog::~Dialog() { delete ui; } bool Dialog::eventFilter(QObject *obj, QEvent *event) { if(obj == butt) { if(event->type() == QEvent::Enter) { effect->setOpacity(1); butt->setGraphicsEffect(effect); } else if(event->type() == QEvent::Leave) { effect->setOpacity(0.5); butt->setGraphicsEffect(effect); } return true; } else { return QDialog::eventFilter(obj, event); } }
Again, when I don't comment the eventfilter parts, the output is an empty dialog window and when I comment those parts, I got:
The reason I use QDesktopWidget is that I want to have my program exe file and then use the program in different computers with different widths and heights, so I have to set geometry to my elements based on the screen geometry.
-
@Pl45m4 Hi. Finally I succeed! I changed the protected part of my header file to:
protected: bool eventFilter(QObject *obj, QEvent *event){ if(obj == butt) { if(event->type() == QEvent::Enter) { effect->setOpacity(1); butt->setGraphicsEffect(effect); } else if(event->type() == QEvent::Leave) { effect->setOpacity(0.5); butt->setGraphicsEffect(effect); } } }
and clear the bool part from the cpp file and my code worked fine.
Thank you so much for the code you provided yesterday. Really appreciate your help .
Best regards. -
I dont see any difference, since the code is the same and you can put your defintions / implementation in your header, but normally you put them in your code (
cpp
) file. You find getters / setters defined in header file very often. So I dont know what was going on before :) It should work with the code in yourcpp
file as well.@nanor said in How to change the QToolButton opacity when the cursor is on int?:
The reason I use QDesktopWidget is that I want to have my program exe file and then use the program in different computers with different widths and heights, so I have to set geometry to my elements based on the screen geometry.
There is nothing wrong with it. I also didn't say, you shouldn't do it, but there will be one day, they remove
QDesktopWidget
completely. It's already kept because of compatibilty reasons. WithQScreen
you can do the same and it will stay in Qt Framework for a longer time thanQDesktopWidget
.
But you really should consider using layouts (you can set a fixed geometry as well as min / max sizes). -
@Pl45m4 I didn't know about QScreen and I thought that the only way to use my device screen geometry is using QDesktopWidget. I will change it to QScreen .
About layouts, also I didn't know that I can set size for them :)
Thank you for valuable points you mentioned. I will definitely use them in my project.