Results 1 to 5 of 5

Thread: Layout Problem

  1. #1
    Join Date
    Feb 2006
    Posts
    42
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Layout Problem

    Hi all I have problem in adding QLabel to the layout of the widget. It never appears on the widget, here is the code I have written. I have derived a class from QMainWindow and here is the code,

    MainWindow::MainWindow()
    {

    first= new QWidget;
    second= new QWidget;
    third= new QWidget;

    middleWindow();

    QHBoxLayout *mainLayout= new QHBoxLayout;
    mainLayout->addWidget(first);
    mainLayout->addWidget(second);
    mainLayout->addWidget(third);
    .
    .
    .
    }

    void MainWindow::middleWindow()
    {
    imageLabel=new QLabel;

    imageLabel->setBackgroundRole(QPalette::Base);
    imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    imageLabel->setScaledContents(true);

    // Load the Centre image

    QImage image("images/middle.bmp");
    imageLabel->setPixmap(QPixmap::fromImage(image));
    QVBoxLayout *d= new QVBoxLayout;
    d->addWidget(imageLabel);
    second->setLayout(d);

    }

    I dont know why label doesnt appear on the second widget. Have I missed anything, in the Layout code?

    Thanks in advance,
    Seema Rao

  2. #2
    Join Date
    Mar 2006
    Location
    Hyderabad
    Posts
    69
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Layout Problem

    hi seema,
    That was quite well. But u have not mentioned the parents for these labels. Try this code:
    Qt Code:
    1. MainWindow::MainWindow()
    2. {
    3.  
    4. first= new QWidget(this);
    5. second= new QWidget(this);
    6. third= new QWidget(this);
    7.  
    8. middleWindow();
    9.  
    10. QHBoxLayout *mainLayout= new QHBoxLayout(this);
    11. mainLayout->addWidget(first);
    12. mainLayout->addWidget(second);
    13. mainLayout->addWidget(third);
    14. .
    15. .
    16. .
    17. }
    18.  
    19. void MainWindow::middleWindow()
    20. {
    21. imageLabel=new QLabel(this);
    22.  
    23. imageLabel->setBackgroundRole(QPalette::Base);
    24. imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    25. imageLabel->setScaledContents(true);
    26.  
    27. // Load the Centre image
    28.  
    29. QImage image("images/middle.bmp");
    30. imageLabel->setPixmap(QPixmap::fromImage(image));
    31. QVBoxLayout *d= new QVBoxLayout(this);
    32. d->addWidget(imageLabel);
    33. second->setLayout(d);
    34. }
    To copy to clipboard, switch view to plain text mode 

    Please let me know what happenned.

    Regards,
    sarma.

  3. #3
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Thanks
    44
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Layout Problem

    You could try calling to show for the label, too
    Last edited by SkripT; 18th April 2006 at 19:29.

  4. #4
    Join Date
    Feb 2006
    Posts
    42
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Post Re: Layout Problem

    Event though I specify the parent as you had said it doesnt work. I have simplified the program and here is the whole code,

    //mainwindow.h
    #include <QMainWindow>
    class QWidget;
    class QLabel;

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    MainWindow();

    private:
    QWidget *first;
    QWidget *second;
    QWidget *third;
    QLabel *imageLabel;
    void middleWindow();
    };


    //mainwindow.cpp
    #include <QtGui>
    #include "mainwindow.h"

    MainWindow::MainWindow()
    {

    first= new QWidget(this);
    second= new QWidget(this);
    third= new QWidget(this);

    middleWindow();

    QHBoxLayout *mainLayout= new QHBoxLayout;
    mainLayout->addWidget(first);
    mainLayout->addWidget(second);
    mainLayout->addWidget(third);

    }

    void MainWindow::middleWindow()
    {

    imageLabel=new QLabel("HILABEL");

    QVBoxLayout *d= new QVBoxLayout(second);
    d->addWidget(imageLabel);
    second->setLayout(d);
    imageLabel->show();

    }

    //main.cpp
    #include <QApplication>
    #include "mainwindow.h"

    int main(int argc, char *argv[])
    {
    QApplication myapp(argc,argv);
    MainWindow mywindow;
    mywindow.show();
    return myapp.exec();
    }

    I dont know why, may be I have to subclass the second widget and in the constructor of that class I have to add the layout that binds the QLabel

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Layout Problem

    - Call base class constructor in MainWindow ctor
    - Where do you apply that mainLayout? Create a "central" widget, set mainLayout as it's layout, and set the "central" widget as mainwindow's central widget.

    Qt Code:
    1. MainWindow::MainWindow() : QMainWindow()
    2. {
    3. ...
    4.  
    5. QHBoxLayout* mainLayout = new QHBoxLayout;
    6. mainLayout->addWidget(first);
    7. mainLayout->addWidget(second);
    8. mainLayout->addWidget(third);
    9.  
    10. QWidget* central = new QWidget;
    11. central->setLayout(mainLayout);
    12. setCentralWidget(central);
    13. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

Similar Threads

  1. Problem in layout spacing
    By mails.hemant in forum Qt Tools
    Replies: 15
    Last Post: 24th November 2008, 12:48
  2. QT Layout problem
    By bod in forum Qt Programming
    Replies: 8
    Last Post: 1st July 2008, 14:50
  3. QT Layout problem
    By bod in forum Qt Programming
    Replies: 1
    Last Post: 1st July 2008, 14:29
  4. problem with Tool button n Layout
    By arjunasd in forum Qt Tools
    Replies: 11
    Last Post: 24th July 2007, 22:14
  5. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 13:45

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.