Results 1 to 10 of 10

Thread: How to set the base widget when subclassing Qt widget

  1. #1
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default How to set the base widget when subclassing Qt widget

    So I need a custom widget which will subclass QFrame. I do the subclassing, and everything works fine. Except that, there is no way way to set this inside a method. As a result, when I call normal methods that are applicable to QFrame on my custom widget, such as setGeometry, it does nothing. Because I have nowhere specified that. How do I specify that? My code:

    Qt Code:
    1. class MyWidget: public QFrame
    2. {
    3. MyWidget(QWidget *parent);
    4.  
    5. private:
    6. prapareUI();
    7. QWidget *parent;
    8. QFrame *baseWidget;
    9. }
    10.  
    11. MyWidget::MyWidget(QWidget *parent): parent(parent) {
    12. prepareUI();
    13. }
    14.  
    15. MyWidget::prepareUI() {
    16. baseWidget = new QFrame(parent);
    17. //do other stuff, add more widgets to base class, it is the container after all
    18. }
    19.  
    20. //main class
    21. MyWidget *widget = new MyWidget(this);
    22. widget->setGeometry(50,50,100,100);
    To copy to clipboard, switch view to plain text mode 

    The widget appears fine as expected, all UI elements added properly inside it. However, because I could not tell main that the setGeometry method on MyWidget should actually call setGeometry internally on the QFrame member variable baseWidget, it does nothing, and it is positioned on the top left corner of the widget by default. How do I fix this? I can extend setGeometry of course, so that it will internally call setGeometry on the baseWidget, but then, this way I will have to redefine many built in methods. Is there no way I can tell the widget that 'this' refers to 'baseWidget'?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to set the base widget when subclassing Qt widget

    BaseWidget is positioned in the top left because you gave it the MyWidget instance as a parent and, I am guessing, did not put it in a layout applied to the MyWidget instance. What is the function of baseWidget? Have you put other widgets inside it instead of inside the MyWidget container? It certainly sounds like that from your questions.

  3. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to set the base widget when subclassing Qt widget

    As I see class MyWidget is inherited from QFrame. So for what is QFrame *baseWidget ?

  4. #4
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default Re: How to set the base widget when subclassing Qt widget

    Quote Originally Posted by Lesiok View Post
    As I see class MyWidget is inherited from QFrame. So for what is QFrame *baseWidget ?
    Yes, that is the problem you see. I want this to be baseWidget. So that the parent of this (or baseWidget) is the parent widget I pass in as parameter. How do I do it?

    Quote Originally Posted by ChrisW67 View Post
    BaseWidget is positioned in the top left because you gave it the MyWidget instance as a parent and, I am guessing, did not put it in a layout applied to the MyWidget instance. What is the function of baseWidget? Have you put other widgets inside it instead of inside the MyWidget container? It certainly sounds like that from your questions.
    Yes, baseWidget will have lots of sub-widgets in it. I pass in the parent because it has to be positioned at a particular position in the parent widget, layout agnostic. If I do

    Qt Code:
    1. QFrame *baseWidget = new QFrame(this);
    2. baseWidget->setGeometry(x,y,width,height);
    3. //add more widgets to baseWidget
    To copy to clipboard, switch view to plain text mode 

    in the parent widget code, it works fine. I want to do the same things, except baseWidget will be of type MyWidget that inherits from QFrame.

  5. #5
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to set the base widget when subclassing Qt widget

    Are you sure you know what is the classe inheritance ?
    Class MyWidget is QFrame too. You do not need field baseWidget and extra WFrame object.

  6. #6
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default Re: How to set the base widget when subclassing Qt widget

    Yes, I know that. I tried this as well:

    Qt Code:
    1. this->setStyleSheet("background: rgb(246,247,239); border-radius: 5px;");
    2. this->setParent(parent);
    3. this->show();
    To copy to clipboard, switch view to plain text mode 

    instead of creating a new QFrame baseWidget. Doesn't work. Doesn't show up at all, unlike the last one where it showed up but not taking the geometry correctly.

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to set the base widget when subclassing Qt widget

    Construct MYWidget so that the content you put in baseWidget is its content, and throw away baseWidget, then you will get what you are expecting.

  8. #8
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default Re: How to set the base widget when subclassing Qt widget

    Yes, I am doing that only. Previously I was doing this:

    Qt Code:
    1. MyWidget::MyWidget(QWidget *parent): parent(parent)
    2. {
    3. prepareUI();
    4. }
    5.  
    6. MyWidget::prepareUI()
    7. {
    8. QFrame *baseWidget = new QFrame(parent);
    9. baseWidget->setStylesheet("...");
    10. QLabel *label = new QLabel(baseWidget);
    11. label->setText("...");
    12. }
    13.  
    14. //in main:
    15.  
    16. MyWidget *widget = new MyWidget(this);
    To copy to clipboard, switch view to plain text mode 

    Now I am doing this:

    Qt Code:
    1. MyWidget::MyWidget(QWidget *parent): parent(parent)
    2. {
    3. prepareUI();
    4. }
    5.  
    6. MyWidget::prepareUI()
    7. {
    8. this->setStylesheet("...");
    9. QLabel *label = new QLabel(this);
    10. label->setText("...");
    11. this->show();
    12. }
    13.  
    14. //in main:
    15.  
    16. MyWidget *widget = new MyWidget(this);
    17. widget->show();
    To copy to clipboard, switch view to plain text mode 

    Still it doesn't show anything at all.


    Added after 7 minutes:


    No no, it's working now. I did not do a clean build, so it was taking the old code..
    Last edited by Cupidvogel; 28th February 2015 at 11:46.

  9. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to set the base widget when subclassing Qt widget

    Nearly there. Now read about Layout Management to get the knowledge needed to have nicely resizable widget.

  10. #10
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default Re: How to set the base widget when subclassing Qt widget

    Thanks. Will do.

Similar Threads

  1. Replies: 0
    Last Post: 3rd August 2011, 13:07
  2. How to get the base parent Widget
    By vaibhav in forum Qt Programming
    Replies: 8
    Last Post: 15th February 2011, 12:27
  3. Subclassing a widget created in Qt Designer
    By dmginc in forum Qt Programming
    Replies: 2
    Last Post: 19th January 2011, 07:30
  4. MVC - Abstract Widget Base Class - setupUI
    By SenSej in forum Newbie
    Replies: 0
    Last Post: 13th October 2008, 11:44
  5. Replies: 2
    Last Post: 14th February 2008, 21:06

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.