Results 1 to 13 of 13

Thread: very basic layout problem

  1. #1
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default very basic layout problem

    this code in the constructor of my QWidget inherited class:


    Qt Code:
    1. QHBoxLayout* hv = new QHBoxLayout(this);
    2. QTableWidget* tv = new QTableWidget(this);
    3. tv->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    4.  
    5. tv->setSelectionBehavior(QAbstractItemView::SelectionBehavior::SelectItems);
    6. tv->setColumnCount(20);
    7. tv->setRowCount(20);
    8.  
    9. hv->addWidget(tv);
    10. setLayout(hv);
    To copy to clipboard, switch view to plain text mode 

    creates the tablewidget crammed in the upperleft corner like this:

    https://i.imgur.com/ZWjY2mE.png


    The tableview that stretches itself over the entire widget as it is supposed to has been created by QtCreator form designer. However, it uses the exact same code! I literally copied over the generated code into my class, but some how only the auto-generated widget stretches properly. Removing the generated one doesnt help either.



    Now, why the hell doesnt it stretch correctly!? Seriously, this shit is driving my crazy.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: very basic layout problem

    Is "this" in the layout of its parent?

    Cheers,
    _

  3. #3
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: very basic layout problem

    no, "this" is a QWidget class


    class MyWidget : public QWidget
    {
    MyWidget(..) : ...
    {
    //code is here
    }
    }

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: very basic layout problem

    Quote Originally Posted by tuli View Post
    no, "this" is a QWidget class
    Yes, that was obvious already. I was asking if the widget has been properly added to its parent's layout.

    Cheers,
    _

  5. #5
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: very basic layout problem

    i directly show the widget with show().

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: very basic layout problem

    So the widget has no parent?

    Cheers,
    _

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: very basic layout problem

    Quote Originally Posted by tuli View Post
    i directly show the widget with show().
    Please show the full code of the widget's constructor. My wild guess would be that you call setupUi() after the code you posted which overrides the widget's layout you set earlier.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: very basic layout problem

    Qt Code:
    1. class test: public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. test(QWidget *parent = 0);
    7. ~test(){}
    8.  
    9. private:
    10. Ui::test ui;
    11. };
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. test::teset(QWidget *parent) : QWidget(parent)
    2. {
    3. ui.setupUi(this);
    4.  
    5. QHBoxLayout* hv = new QHBoxLayout(this);
    6. QTableWidget* rt = new QTableWidget(this);
    7.  
    8. rt->setColumnCount(4);
    9. rt->setRowCount(10);
    10. rt->setRowHeight(0, 22);
    11. rt->verticalHeader()->hide();
    12.  
    13. hv->addWidget(rt);
    14. setLayout(hv);
    15. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: very basic layout problem

    What happens if you comment out ui.setupUi(this) ?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: very basic layout problem

    If your designer UI is empty, which it must be since none of its contents would be added to the layout, why have the instance at all?

    Cheers,
    _

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: very basic layout problem

    Quote Originally Posted by anda_skoa View Post
    If your designer UI is empty, which it must be since none of its contents would be added to the layout, why have the instance at all?
    I think the point is that it is not empty and in the image we see two separate sets of widgets (with one widget in each set, actually) stacked on top of each other.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: very basic layout problem

    you are right. I am not sure what exactly the problem was, but removing all the designer stuff makes it work.


    Essentially we end up with this:


    class abc : public QWidget
    {
    public:
    abc(QWidget* p = 0) : QWidget(p)
    {
    QHBoxLayout* lay = new QHBoxLayout(this);
    QTableWidget* tw = new QTableWidget();
    tw->setColumnCount(8);
    tw->setRowCount(20);
    lay->addWidget(tw);
    setLayout(lay);
    }
    };

    and that works fine. thanks for your help!

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: very basic layout problem

    Quote Originally Posted by tuli View Post
    you are right. I am not sure what exactly the problem was,
    The problem was two calls to setLayout().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. The following user says thank you to wysota for this useful post:

    tuli (20th August 2014)

Similar Threads

  1. basic opengl problem
    By Gravis in forum Qt Programming
    Replies: 1
    Last Post: 22nd January 2010, 11:55
  2. Basic hash problem
    By feraudyh in forum Newbie
    Replies: 6
    Last Post: 4th December 2009, 09:59
  3. QT 4.5 + .config problem with Basic XLib?
    By qoo in forum Installation and Deployment
    Replies: 4
    Last Post: 5th August 2009, 12:52
  4. basic problem of config qmake
    By Shuchi Agrawal in forum Qt Tools
    Replies: 10
    Last Post: 10th January 2007, 10:31
  5. basic qt opengl problem
    By kingslee in forum Qt Programming
    Replies: 4
    Last Post: 12th November 2006, 16:26

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.