View Full Version : Layout Problem
Seema Rao
18th April 2006, 15:38
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
Sarma
18th April 2006, 17:01
hi seema,
That was quite well. But u have not mentioned the parents for these labels. Try this code:
MainWindow::MainWindow()
{
first= new QWidget(this);
second= new QWidget(this);
third= new QWidget(this);
middleWindow();
QHBoxLayout *mainLayout= new QHBoxLayout(this);
mainLayout->addWidget(first);
mainLayout->addWidget(second);
mainLayout->addWidget(third);
.
.
.
}
void MainWindow::middleWindow()
{
imageLabel=new QLabel(this);
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(this);
d->addWidget(imageLabel);
second->setLayout(d);
}
Please let me know what happenned.
Regards,
sarma.
SkripT
18th April 2006, 17:08
You could try calling to show for the label, too
Seema Rao
19th April 2006, 09:52
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
jpn
19th April 2006, 11:08
- 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 (http://doc.trolltech.com/4.1/qmainwindow.html#setCentralWidget).
MainWindow::MainWindow() : QMainWindow()
{
...
QHBoxLayout* mainLayout = new QHBoxLayout;
mainLayout->addWidget(first);
mainLayout->addWidget(second);
mainLayout->addWidget(third);
QWidget* central = new QWidget;
central->setLayout(mainLayout);
setCentralWidget(central);
}
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.