Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8347353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:20:21+00:00 2026-06-09T07:20:21+00:00

I’d like to make a single widget that combines the behavior of two existing

  • 0

I’d like to make a single widget that combines the behavior of two existing widgets. For example a widget that is a button that also contains a checkbox. The user can click the larger button but also the checkbox within.

E.g. gmail introduced a “select-all” button that displays a menu on click, but it also integrates a checkbox that selects all email. See this screenshot for an example:
enter image description here

How would one go about combining behaviors of existing widgets to this effect? I’m hoping for a solution where widget behaviors can be combined and I only need to override where they conflict.

This question is similar to the Combine multiple widgets into one in qt question but differs in that I want the two widgets to be placed on top of each other and appear as one widget.

In response to cbamber85’s reply I’ve added the code below to show how far I’ve managed to integrate the replies. As with cbamber85’s reply this contains no real functionality, it’s just the PyQT version of his code.

from PyQt4 import QtCore, QtGui

class CheckBoxButton(QtGui.QWidget):
    checked = QtCore.pyqtSignal(bool)

    def __init__(self, *args):
        QtGui.QWidget.__init__(self, *args)
        self._isdown = False
        self._ischecked = False

    def isChecked(self):
        return self._ischecked

    def setChecked(self, checked):
        if self._ischecked != checked:
            self._ischecked = checked
            self.checked.emit(checked)

    def sizeHint(self, *args, **kwargs):
        QtGui.QWidget.sizeHint(self, *args, **kwargs)
        return QtCore.QSize(128,128)

    def paintEvent(self, event):
        p = QtGui.QPainter(self)
        butOpt = QtGui.QStyleOptionButton()
        butOpt.initFrom(self)
        butOpt.state = QtGui.QStyle.State_Enabled
        butOpt.state |= QtGui.QStyle.State_Sunken if self._isdown else QtGui.QStyle.State_Raised

        self.style().drawControl(QtGui.QStyle.CE_PushButton, butOpt, p, self)

        chkBoxWidth = self.style().pixelMetric(QtGui.QStyle.PM_CheckListButtonSize,
                                               butOpt, self) / 2
        butOpt.rect.moveTo((self.rect().width() / 2) - chkBoxWidth, 0)

        butOpt.state |= QtGui.QStyle.State_On if self._ischecked else QtGui.QStyle.State_Off
        self.style().drawControl(QtGui.QStyle.CE_CheckBox, butOpt, p, self)

    def mousePressEvent(self, event):
        self._isdown = True
        self.update()

    def mouseReleaseEvent(self, event):
        self.setChecked(not self.isChecked())
        self._isdown = False
        self.update()


class Dialog(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        widg = CheckBoxButton(self)
        self.setCentralWidget(widg)


if __name__ == '__main__':
    app = QtGui.QApplication([])
    window = Dialog()
    window.show()
    app.exec_()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T07:20:22+00:00Added an answer on June 9, 2026 at 7:20 am

    Yes of course you can, but how easy it is depends how the widgets are to be combined.

    For example if you want to create a button that has a label next to it rather than inside, you derive from QWidget, add your QLabel and QPushButton as members and then simply append them to the widget’s internal layout in the constructor. (I should point out that there are many different ways of doing this).

    However, for widgets that combine behaviours into a new, visually different widget – you need to tell Qt how to paint it. So subclass paintEvent(..) and draw your widget manually onto a QPainter(this). And to match the widget to the current style, you’ll have to get QStyle to do the component drawing for you – which actually makes life a lot easier.

    Then you need to override mouse###Event(..) and manually handle mouse interaction (and may want keyboard interaction too).

    It’s laborious, but naturally there’s less work the closer your widget is to an existing one. In your case I would derive from QToolButton as it already supports drop down menus. Then reimplement paintEvent(..), call QStyle::drawControl(CE_PushButtonBevel, ... to draw the button, and then QStyle::drawControl(CE_CheckBox, ... to draw the checkbox in the correct state. Then reimplement mousePressEvent(..), work out if within the checkbox hit area, and change state accordingly.

    A Ridiculous Example

    This pointless widget demonstrates some of the techniques described above.

    class WeirdCheckBoxButton : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit WeirdCheckBoxButton( QWidget* parent = nullptr )
                 : QWidget( parent ), checked_( false ), down_( false ) {}
        virtual ~WeirdCheckBoxButton() {}
    
        bool isChecked() const { return checked_; }
        QSize sizeHint () const { return QSize( 128, 128 ); }
    
    public slots:
        virtual void setChecked( bool checked )
        {
            if ( checked_ != checked ) {
                checked_ = checked;
                emit clicked( checked );
            }
        }
    
    signals:
        void clicked( bool checked );
    
    protected:
        virtual void paintEvent( QPaintEvent* event )
        {
            Q_UNUSED( event )
            QPainter p( this );
    
            //  QStyleOption, and derived classes, describe the state of the widget.
            QStyleOptionButton butOpt;
            butOpt.initFrom( this ); // A convenience function that steals state
                                     // from this widget.
            butOpt.state = QStyle::State_Enabled;
            butOpt.state |= down_ ? QStyle::State_Sunken : QStyle::State_Raised;
    
            //  Renders the widget onto the QPainter.
            style()->drawControl( QStyle::CE_PushButton, &butOpt, &p, this );
    
            //  pixelMetric() gives you style relevant dimensional data.
            int chkBoxWidth = style()->pixelMetric( QStyle::PM_CheckListButtonSize,
                                                    &butOpt, this ) / 2;
            butOpt.rect.moveTo( ( rect().width() / 2 ) - chkBoxWidth, 0 );
            butOpt.state |= checked_ ? QStyle::State_On : QStyle::State_Off;
            style()->drawControl( QStyle::CE_CheckBox, &butOpt, &p, this );
        }
    
        virtual void mousePressEvent( QMouseEvent* event )
        {
            Q_UNUSED( event )
    
            down_ = true;
            update();
        }
    
        virtual void mouseReleaseEvent( QMouseEvent* event )
        {
            Q_UNUSED( event )
    
            setChecked( !isChecked() );
            down_ = false;
            update();
        }
    
    private:
        bool checked_;
        bool down_;
    };
    

    It’s a thin example, but it’s ripe for copying and experimenting on. For example by accessing butOpt.palette, you can tweak the colours of rendered widgets depending on their state.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I have two tables with like below codes: Table: Accounts id | username |
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.