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. Clicked signal and slot for a QStyleOptionButton drawn inside paintEvent
Forum Updated to NodeBB v4.3 + New Features

Clicked signal and slot for a QStyleOptionButton drawn inside paintEvent

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 673 Views 2 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.
  • H Offline
    H Offline
    hbatalha
    wrote on last edited by
    #1

    I have a widget where I draw this button:

    void QWidget::paintEvent(QPaintEvent *event)
    {
        QIcon icon = qApp->style()->standardIcon(QStyle::SP_MessageBoxCritical);
        QStyleOptionButton opt;
        opt.state = QStyle::State_Active | QStyle::State_Enabled;
        opt.rect = QRect(50, 25, 100, 50);
        QPainter painter(this);
        painter.drawText(MAIN.height() / 2, 30, 80, 30, Qt::AlignCenter, "");
        painter.beginNativePainting();
        qApp->style()->drawControl(QStyle::CE_PushButton,&opt,&painter);
        painter.endNativePainting();
    
        QWidget::paintEvent(event);
    }
    

    I want to know how would I connect it to a slot when it's clicked, if that's possible.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      If you want a QPushButton why don't you simply use one instead trying to paint it by yourself?

      If you really want to handle a click on a widget by yourself, you have to overwrite QWidget::mousePressEvent()

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      H 1 Reply Last reply
      3
      • Christian EhrlicherC Christian Ehrlicher

        If you want a QPushButton why don't you simply use one instead trying to paint it by yourself?

        If you really want to handle a click on a widget by yourself, you have to overwrite QWidget::mousePressEvent()

        H Offline
        H Offline
        hbatalha
        wrote on last edited by
        #3

        @Christian-Ehrlicher Because the button has to drawn inside paintEvent, like everything else in the widget

        M Christian EhrlicherC 2 Replies Last reply
        0
        • H hbatalha

          @Christian-Ehrlicher Because the button has to drawn inside paintEvent, like everything else in the widget

          M Offline
          M Offline
          mpergand
          wrote on last edited by mpergand
          #4

          @hbatalha
          If you use QAbstractButton as the base class, the following signals are available:

          void clicked(bool checked = false)
          void pressed()
          void released()
          void toggled(bool checked)

          as well as standard button states: checkable, checked, pressed (isDown) etc.

          1 Reply Last reply
          0
          • H hbatalha

            @Christian-Ehrlicher Because the button has to drawn inside paintEvent, like everything else in the widget

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @hbatalha said in Clicked signal and slot for a QStyleOptionButton drawn inside paintEvent:

            Because the button has to drawn inside paintEvent, like everything else in the widget

            This does not make any sense to me. Why needs a widget paint a QPushButton? Why can't you just simply use a QPushButton widget?

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            H 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              @hbatalha said in Clicked signal and slot for a QStyleOptionButton drawn inside paintEvent:

              Because the button has to drawn inside paintEvent, like everything else in the widget

              This does not make any sense to me. Why needs a widget paint a QPushButton? Why can't you just simply use a QPushButton widget?

              H Offline
              H Offline
              hbatalha
              wrote on last edited by hbatalha
              #6

              @Christian-Ehrlicher sorry. that's true, that doesn't make sense. I was thinking about it in a different way ( a wrong way btw).
              Using the regular qpushbutton does the job.

              1 Reply Last reply
              1

              • Login

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