Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to change the QToolButton opacity when the cursor is on int?
Forum Updated to NodeBB v4.3 + New Features

How to change the QToolButton opacity when the cursor is on int?

Scheduled Pinned Locked Moved Solved General and Desktop
22 Posts 2 Posters 2.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N nanor

    @Pl45m4 I'm really sorry , if I understood correctly, I have to remove the line void enterEvent(QEvent * event); from my header file?

    Pl45m4P Offline
    Pl45m4P Offline
    Pl45m4
    wrote on last edited by Pl45m4
    #10

    @nanor said in How to change the QToolButton opacity when the cursor is on int?:

    remove the line void enterEvent(QEvent * event);

    Or comment it to test if my code works for you. You dont need a QDialog::EnterEvent. AND move the eventFilter implementation to your cpp. The errors should be gone now.


    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

    ~E. W. Dijkstra

    N 1 Reply Last reply
    0
    • Pl45m4P Pl45m4

      @nanor said in How to change the QToolButton opacity when the cursor is on int?:

      remove the line void enterEvent(QEvent * event);

      Or comment it to test if my code works for you. You dont need a QDialog::EnterEvent. AND move the eventFilter implementation to your cpp. The errors should be gone now.

      N Offline
      N Offline
      nanor
      wrote on last edited by nanor
      #11

      @Pl45m4 I commented the line void enterEvent(QEvent * event); in the header file and added bool eventFilter(QObject *obj, QEvent *event) override; to the cpp file, after ui->setupUi(this);
      in the constructor, but I get this errer:

      sssss.png

      Pl45m4P 1 Reply Last reply
      0
      • N nanor

        @Pl45m4 I commented the line void enterEvent(QEvent * event); in the header file and added bool eventFilter(QObject *obj, QEvent *event) override; to the cpp file, after ui->setupUi(this);
        in the constructor, but I get this errer:

        sssss.png

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by
        #12

        @nanor

        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);
            }
        

        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        N 1 Reply Last reply
        1
        • Pl45m4P Pl45m4

          @nanor

          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);
              }
          
          N Offline
          N Offline
          nanor
          wrote on last edited by
          #13

          @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:

          aaaa.png

          Pl45m4P 1 Reply Last reply
          0
          • N nanor

            @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:

            aaaa.png

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by Pl45m4
            #14

            @nanor

            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".


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            N 1 Reply Last reply
            0
            • Pl45m4P Pl45m4

              @nanor

              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".

              N Offline
              N Offline
              nanor
              wrote on last edited by
              #15

              @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.

              emty.png

              Pl45m4P 1 Reply Last reply
              0
              • N nanor

                @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.

                emty.png

                Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on last edited by
                #16

                @nanor

                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 some QLayout (horizontal or vertical) to your QDialog and then add your button.


                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                N 1 Reply Last reply
                0
                • Pl45m4P Pl45m4

                  @nanor

                  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 some QLayout (horizontal or vertical) to your QDialog and then add your button.

                  N Offline
                  N Offline
                  nanor
                  wrote on last edited by
                  #17

                  @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:

                  asd.png

                  And when I don't comment those parts, I got an empty dialog window.

                  Pl45m4P 1 Reply Last reply
                  0
                  • N nanor

                    @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:

                    asd.png

                    And when I don't comment those parts, I got an empty dialog window.

                    Pl45m4P Offline
                    Pl45m4P Offline
                    Pl45m4
                    wrote on last edited by
                    #18

                    @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.html

                    Your 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 (after butt->setCursor(Qt::PointingHandCursor);)

                    QVBoxLayout *layout = new QVBoxLayout;
                    layout->addWidget(butt);
                    setLayout(layout);
                    

                    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                    ~E. W. Dijkstra

                    N 1 Reply Last reply
                    0
                    • Pl45m4P Pl45m4

                      @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.html

                      Your 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 (after butt->setCursor(Qt::PointingHandCursor);)

                      QVBoxLayout *layout = new QVBoxLayout;
                      layout->addWidget(butt);
                      setLayout(layout);
                      
                      N Offline
                      N Offline
                      nanor
                      wrote on last edited by
                      #19

                      @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:

                      vboxxx.png

                      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.

                      N 1 Reply Last reply
                      0
                      • N nanor

                        @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:

                        vboxxx.png

                        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.

                        N Offline
                        N Offline
                        nanor
                        wrote on last edited by
                        #20

                        @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.

                        Pl45m4P 1 Reply Last reply
                        0
                        • N nanor

                          @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.

                          Pl45m4P Offline
                          Pl45m4P Offline
                          Pl45m4
                          wrote on last edited by Pl45m4
                          #21

                          @nanor

                          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 your cpp 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. With QScreen you can do the same and it will stay in Qt Framework for a longer time than QDesktopWidget.
                          But you really should consider using layouts (you can set a fixed geometry as well as min / max sizes).


                          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                          ~E. W. Dijkstra

                          N 1 Reply Last reply
                          0
                          • Pl45m4P Pl45m4

                            @nanor

                            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 your cpp 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. With QScreen you can do the same and it will stay in Qt Framework for a longer time than QDesktopWidget.
                            But you really should consider using layouts (you can set a fixed geometry as well as min / max sizes).

                            N Offline
                            N Offline
                            nanor
                            wrote on last edited by
                            #22

                            @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.

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved