PDA

View Full Version : Qt Widgets appearing small on Main Window



souvikkar.kar
9th September 2012, 16:09
I am fairly new in gui programming. For my current project i am using qt to develop symbian applications. However when i add any widget in the main window using qt designer or manually writing them, the widgets appear to be too tiny...

Currently i want to show an image using q label, alongwith a toolbar. The toolbar shows fine, but the label appears tiny.

`MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)//, ui(new Ui::MainWindow)
{
//ui->setupUi(this);
label= new QLabel(this);
image=new QImage(":/Images/lena.jpg");
label->setPixmap(QPixmap::fromImage((*image)));
label->setScaledContents(true);
label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
toolbar=new QToolBar(this);
QToolButton *load=new QToolButton();
QToolButton *gray=new QToolButton();
QToolButton *bin=new QToolButton();
QToolButton *bye=new QToolButton();
QToolButton *save=new QToolButton();
QToolButton *reset=new QToolButton();
/*layout=new QVBoxLayout(this);
layout->addWidget(label);
layout->addWidget(toolbar);*/

load->setIcon(QIcon(":/Icons/new_file.png"));
gray->setIcon(QIcon(":/Icons/grayscale.png"));
bin->setIcon(QIcon(":/Icons/bit_clock.png"));
bye->setIcon(QIcon(":/Icons/exit.png"));
save->setIcon(QIcon(":/Icons/save.png"));
reset->setIcon(QIcon(":/Icons/reset.png"));
//label->show();

addToolBar(Qt::BottomToolBarArea, toolbar);

toolbar->addWidget(load);
toolbar->addWidget(gray);
toolbar->addWidget(bin);
toolbar->addWidget(save);
toolbar->addWidget(reset);
toolbar->addWidget(bye);

load->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
gray->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
bin->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
bye->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
save->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
reset->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
toolbar->setMovable(false);
toolbar->setIconSize(QSize(35,35));

connect(load, SIGNAL(clicked()), this, SLOT(loadNewImage()));
connect(gray, SIGNAL(clicked()), this, SLOT(loadGrayImage()));
connect(bin, SIGNAL(clicked()), this, SLOT(loadBinImage()));
connect(bye, SIGNAL(clicked()), this, SLOT(close()));
connect(save, SIGNAL(clicked()), this, SLOT(saveImage()));
connect(reset, SIGNAL(clicked()), this, SLOT(resetImage()));
//setCentralWidget(label);
//this->setLayout(layout);
//this->showExpanded();
}`

The portions i have blocked using '//' are not helping either. i have tried to make a layout, but it comes to no avail. Using setcentralwidget results in showing just d label and no toolbar. i have blocked qt designer portions as i am not using them. I am attaching a pic showing d output.

8203

wysota
9th September 2012, 17:23
You need a layout on the central widget (and add your label there) and you don't want setScaledContents(true) on the label.

souvikkar.kar
10th September 2012, 18:11
Thanx for the reply.
I edited the code as follows:


setCentralWidget(label);
layout=new QVBoxLayout(this);
layout->addWidget(label);
layout->addWidget(toolbar);

also i made setScaledComponents(false);
now instead of d small vertion of d entire image, i am getting a tiny cropped portion of d original image,i.e, the previous problem still persists...

wysota
10th September 2012, 20:50
Please read the docs on QMainWindow to see what the central widget is and how are toolbars related to the rest of the main window family and friends. Your current code makes completely no sense.

souvikkar.kar
11th September 2012, 08:34
Sorry, I am absolutely new to any kind of gui programming. It will be really helpful if u give a code snippet to show how i can solve this particular problem.

I will also be thankful if u can show a small example of a QMainwindow containing menubar, toolbar, central widget and dockwidget. Thanx in advance... :)

wysota
11th September 2012, 08:50
The proper equivalent of your code is something like:

QLabel *label = new QLabel;
label->setPixmap(QPixmap(":/lena.jpg"));
QToolBar *toolbar = addToolBar("my tool bar");
toolbar->addAction(QIcon(":/Icons/new_file.png"), "load", this, SLOT(loadNewImage()));
// etc.
setCentralWidget(label);

souvikkar.kar
11th September 2012, 11:30
Thanx for d quick reply. the addToolBar function you have used places d toolbar at d top(However Now it works, the image shows properly). But I want to place d toolbar at d bottom. So thats a problem. I tried using addtoolbar fn i have originally used, but then the centralwidget overlaps d toolbar. Is there a way i can add the toolbar at d bottom and prevent this? Also, when i load a high resolution image, the label extends beyond the screen and the toolbar is not completely visible. Do i use a QScrollBarArea to prevent this? If yes, how? I really dont want to include a scroll area. Is there a way i can resize the label so that it fits into my screen properly?

And previously i used toolbuttons instead of addAction() as i wanted the toolbar icons rearrange themselves properly according to the screen orientation. So i set d size policy of each button...

Thanx in advance...

wysota
11th September 2012, 14:51
Thanx for d quick reply. the addToolBar function you have used places d toolbar at d top(However Now it works, the image shows properly). But I want to place d toolbar at d bottom.
So move it to the bottom. There are many variants of addToolBar(), use the one that fits your usecase best.


So thats a problem. I tried using addtoolbar fn i have originally used, but then the centralwidget overlaps d toolbar.

QToolBar *toolbar = new QToolBar;
addToolBar(Qt::BottomToolBarArea, toolbar);
// ...



And previously i used toolbuttons instead of addAction() as i wanted the toolbar icons rearrange themselves properly according to the screen orientation.
I don't see how are the two related to each other.

souvikkar.kar
11th September 2012, 16:39
If you see my first post, i basically did everything you just said except using actions on d toolbar... I used d variant of addToolBar() you just said. i also made label as central widget.

My code is as follows:


MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
label= new QLabel(this);
image=new QImage(":/Images/lena.jpg");
label->setPixmap(QPixmap::fromImage((*image)));


toolbar=new QToolBar(this);

QToolButton *load=new QToolButton();
QToolButton *gray=new QToolButton();
QToolButton *bin=new QToolButton();
QToolButton *bye=new QToolButton();
QToolButton *save=new QToolButton();
QToolButton *reset=new QToolButton();


load->setIcon(QIcon(":/Icons/new_file.png"));
gray->setIcon(QIcon(":/Icons/grayscale.png"));
bin->setIcon(QIcon(":/Icons/bit_clock.png"));
bye->setIcon(QIcon(":/Icons/exit.png"));
save->setIcon(QIcon(":/Icons/save.png"));
reset->setIcon(QIcon(":/Icons/reset.png"));


toolbar->addWidget(load);
toolbar->addWidget(gray);
toolbar->addWidget(bin);
toolbar->addWidget(save);
toolbar->addWidget(reset);
toolbar->addWidget(bye);

addToolBar(Qt::BottomToolBarArea, toolbar);

/*Setting horizontal size policy for each tool button ensures that whenever screen orientation is changed
it takes up all available horizontal space*/

load->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
gray->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
bin->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
bye->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
save->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
reset->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);

toolbar->setMovable(false);
toolbar->setIconSize(QSize(35,35));

connect(load, SIGNAL(clicked()), this, SLOT(loadNewImage()));
connect(gray, SIGNAL(clicked()), this, SLOT(loadGrayImage()));
connect(bin, SIGNAL(clicked()), this, SLOT(loadBinImage()));
connect(bye, SIGNAL(clicked()), qApp, SLOT(quit()));
connect(save, SIGNAL(clicked()), this, SLOT(saveImage()));
connect(reset, SIGNAL(clicked()), this, SLOT(resetImage()));
setCentralWidget(label);

}

With this code, this is the output:

8212

As u see, the label is overlapping d toolbar, how to stop this without using Qscrollarea? :confused::confused::confused:

If you see my first post, i basically did everything you just said except using actions on d toolbar... I used d variant of addToolBar() you just said. i also made label as central widget.

My code is as follows:


MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
label= new QLabel(this);
image=new QImage(":/Images/lena.jpg");
label->setPixmap(QPixmap::fromImage((*image)));


toolbar=new QToolBar(this);

QToolButton *load=new QToolButton();
QToolButton *gray=new QToolButton();
QToolButton *bin=new QToolButton();
QToolButton *bye=new QToolButton();
QToolButton *save=new QToolButton();
QToolButton *reset=new QToolButton();


load->setIcon(QIcon(":/Icons/new_file.png"));
gray->setIcon(QIcon(":/Icons/grayscale.png"));
bin->setIcon(QIcon(":/Icons/bit_clock.png"));
bye->setIcon(QIcon(":/Icons/exit.png"));
save->setIcon(QIcon(":/Icons/save.png"));
reset->setIcon(QIcon(":/Icons/reset.png"));


toolbar->addWidget(load);
toolbar->addWidget(gray);
toolbar->addWidget(bin);
toolbar->addWidget(save);
toolbar->addWidget(reset);
toolbar->addWidget(bye);

addToolBar(Qt::BottomToolBarArea, toolbar);

/*Setting horizontal size policy for each tool button ensures that whenever screen orientation is changed
it takes up all available horizontal space*/

load->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
gray->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
bin->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
bye->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
save->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
reset->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);

toolbar->setMovable(false);
toolbar->setIconSize(QSize(35,35));

connect(load, SIGNAL(clicked()), this, SLOT(loadNewImage()));
connect(gray, SIGNAL(clicked()), this, SLOT(loadGrayImage()));
connect(bin, SIGNAL(clicked()), this, SLOT(loadBinImage()));
connect(bye, SIGNAL(clicked()), qApp, SLOT(quit()));
connect(save, SIGNAL(clicked()), this, SLOT(saveImage()));
connect(reset, SIGNAL(clicked()), this, SLOT(resetImage()));
setCentralWidget(label);

}

With this code, this is the output:

8212

As u see, the label is overlapping d toolbar, how to stop this without using Qscrollarea? :confused::confused::confused:

Added after 8 minutes:


So move it to the bottom. There are many variants of addToolBar(), use the one that fits your usecase best.



QToolBar *toolbar = new QToolBar;
addToolBar(Qt::BottomToolBarArea, toolbar);
// ...



I don't see how are the two related to each other.


If I use addAction, this is d output for landscape mode: (I didnt use central widget otherwise toolbar is overlapped)
8213

Using toolbutton and setting size policy for each button this is d output:
8214

wysota
11th September 2012, 19:48
Don't use the toolbar. Use a row of tool buttons instead. Since you are forcing manually created widgets into the toolbar then what is the point of having that toolbar there?

souvikkar.kar
11th September 2012, 20:23
Back to d original topic, how then can i solve my problem? central widget still overlapping on toolbar.:eek::confused:

wysota
11th September 2012, 21:09
I told you -- get rid of the toolbar in favour of a widget composed of the label and a row of toolbuttons.

souvikkar.kar
12th September 2012, 06:55
So, i decided to use setFixedSize(), as u have suggested in
http://www.qtcentre.org/threads/2593-Auto-resize-QLabel
Now i need to know d native screen resolution of d phone and current screen orientation.How to do this?:confused: That will help me in deciding the size...
Also is there a way, i can place d pixmap at the centre of d label?:)

wysota
12th September 2012, 08:11
Man.... just learn to use layouts properly and all your problems will go away....