Results 1 to 3 of 3

Thread: Widgets in QGridLayout placed at top left

  1. #1
    Join Date
    Sep 2010
    Location
    Iran
    Posts
    2
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Widgets in QGridLayout placed at top left

    Hi, It's my first time trying Qt and followed the tutorials on widgets first, then the address book tutorial. My problem is with arranging widgets in a QGridLayout, I use proper row/column numbers but when I run the program, all of them are palced at top left.

    /Here's the code\
    adressbook.hpp:
    Qt Code:
    1. #include <QWidget>
    2. #include <QLineEdit>
    3. #include <QTextEdit>
    4.  
    5. class address_book: public QWidget {
    6. Q_OBJECT
    7. QLineEdit name;
    8. QTextEdit address;
    9.  
    10. public:
    11. address_book(QWidget *parent=0);
    12. };
    To copy to clipboard, switch view to plain text mode 

    addressbook.cpp:
    Qt Code:
    1. #include "addressbook.hpp"
    2. #include <QLabel>
    3. #include <QGridLayout>
    4.  
    5. address_book::address_book(QWidget *parent): QWidget(parent), name(), address() {
    6. QLabel lname(tr("Name:"));
    7. QLabel laddress(tr("Address:"));
    8.  
    9. QGridLayout layout(this);
    10. layout.setSpacing(10);
    11.  
    12. layout.addWidget(&lname, 0, 0);
    13. layout.addWidget(&name, 0, 1);
    14.  
    15. layout.addWidget(&laddress, 1, 0);
    16. layout.addWidget(&address, 1, 1);
    17.  
    18. setWindowTitle(tr("Address Book"));
    19. }
    To copy to clipboard, switch view to plain text mode 
    Here's the screenshot:
    app.jpg

    What might be causing this behaviour?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Widgets in QGridLayout placed at top left

    Create your layout on the heap, not on the stack! Right now it will be deleted after the constructor, so that in the end the widgets are not layouted (since their layout is getting deleted...)

  3. The following user says thank you to Lykurg for this useful post:

    anarion (2nd September 2010)

  4. #3
    Join Date
    Sep 2010
    Location
    Iran
    Posts
    2
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Widgets in QGridLayout placed at top left

    damn... wasn't looking at it from that aspect
    Thanks! it works.

Similar Threads

  1. Replies: 4
    Last Post: 29th August 2010, 18:16
  2. qgridlayout and hiding widgets
    By Michael_Levin in forum Newbie
    Replies: 3
    Last Post: 21st May 2010, 17:47
  3. space between widgets in qgridlayout
    By demol in forum Qt Programming
    Replies: 2
    Last Post: 6th January 2010, 20:07
  4. Delete a QGridLayout and New QGridLayout at runtime
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 5th November 2007, 13:01
  5. QGridLayout
    By ToddAtWSU in forum Qt Programming
    Replies: 5
    Last Post: 29th June 2006, 20:34

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.