Results 1 to 4 of 4

Thread: Add a custom widget to a "stock" Qt widget e.g. QSpinBox

  1. #1
    Join Date
    May 2012
    Posts
    2

    Default Add a custom widget to a "stock" Qt widget e.g. QSpinBox

    Hello,

    I want to extend a "stock" Qt widget with a custom widget.

    For example I want to add a additional button to a spinbox.

    To archive this i used the public accessible setLayout() function. The result is not what i epected. I want to add the button left or right of the standard QSpinBox.

    See this small snippet.


    Qt Code:
    1. #include <QApplication>
    2. #include <QHBoxLayout>
    3. #include <QPushButton>
    4. #include <QSpinBox>
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication a(argc, argv);
    9.  
    10.  
    11. QHBoxLayout * hbl = new QHBoxLayout();
    12. hbl->addWidget( new QPushButton("Hello"));
    13. s.setLayout(hbl);
    14.  
    15. s.show();
    16.  
    17. return a.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 

    qt.jpg

    In my real code I have extended the "stock" Qt widget and therefore can also access the protected members.

    Hoiw can i access the "real" layout of QSpinBox.

    Is there another solution to archive my goal?

    Greetings Gerd

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Add a custom widget to a "stock" Qt widget e.g. QSpinBox

    You aren't "extending" QSpinBox with this code. You are taking a standard QSpinBox and messing up its UI, as your screenshot shows. Adding the horizontal layout does not move the spin box into it, it simply causes both widgets to paint on top of each other.

    There are two possible solutions:

    1 - Derive a new class, using QSpinBox (or QAbstractSpinBox)as a base class. You will have to reimplement much of the class, especially layout, painting, and response to mouse actions because the QSpinBox geometry will be different. I do not recommend doing this. It's way more work than you need, and you will be spending all of your time trying to debug it.

    2 - Create a new widget using QWidget or maybe QFrame as a base class. In that class, add your horizontal layout, then add the button and spin box to the layout. If you want to make your new widget act like a combination of a QSpinBox and a QPushbutton, then add signals to your new class to relay the signals from the two widgets inside it. In the constructor for your new widget, connect the signals from the button and spin box to the corresponding signals in your new class. You can also add slots to your widget which pass data into the button and spin box. I would recommend this approach.

    If you go for option 2, it would be a real mistake to make the spin box and button member variables public. The whole point of deriving new classes is to hide the implementation from the outside world, and you do that by mapping signals, slots, and methods from the child widgets out to the new class interface instead.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    May 2012
    Posts
    2

    Default Re: Add a custom widget to a "stock" Qt widget e.g. QSpinBox

    Hello,

    Thanks for your answer.

    As far as I understand your answer it is not wanted by design to modify "stock" widgets. But why do they leave setLayout() accessible then?

    For my current task i just want to add a button to a spinbox. The button will open a formula edit dialog. after editing the formula the result will be set to the spinbox. I do not want to make complex custom widgets and just want to add a simple button.

    I was aware of your option 2. But a custom widget with QWidget as base does not integrate nice with designer: Usually i promote the custom widgets in designer. With QWidget as base there are no more attributes setable in designer and it is visually just a empty area.

    Generally there is also a option 3: Writing a custom widget plugin. I use the mingw compiler, because of that writing custom widget plugins is also not easy. In order to write custom widget plugins I first have to compile Creator and Designer with mingw. The last time i checkd this topic i recognized that Creator and Designer is compiled with VC++ and i do not think that this has changed now.

    In order to keep it easy i decided to do a custom "formula edit" button whith an setter for an spinbox pointer.

    When you know another choice to create custom widgets which are usable in designer please let me know about it.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Add a custom widget to a "stock" Qt widget e.g. QSpinBox

    I was aware of your option 2. But a custom widget with QWidget as base does not integrate nice with designer: Usually i promote the custom widgets in designer. With QWidget as base there are no more attributes setable in designer and it is visually just a empty area.
    Sometimes you just have to bite the bullet and write some real code to create a GUI instead of dragging and dropping. Yes, you can write a custom widget plugin for Qt Designer, but unless you are developing a library of widgets (generally for others to use), this is overkill.

    If you write a custom widget, you can add it to your UI in Qt Designer, promote it, and then set the properties you want in code after the setupUi() call in the parent widget's constructor. It isn't that much more work.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 0
    Last Post: 18th February 2015, 21:45
  2. Replies: 1
    Last Post: 27th November 2012, 10:47
  3. Replies: 2
    Last Post: 20th March 2010, 19:22
  4. Replies: 7
    Last Post: 21st January 2010, 13:45
  5. Replies: 2
    Last Post: 25th August 2006, 12:35

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.