PDA

View Full Version : Help with alignment



IsleWitch
16th October 2007, 07:30
Hi,

Im having trouble in aligning my QComboBox, and my exit button. I want them to be like the image that i have attached (in the center, in the right hand corner of the window), i have aligned them, but i must've did something wrong.

Here is my code:


#include <QLabel>
#include <QPushButton>
#include <QGraphicsView>
#include <QLineEdit>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QApplication>
#include <QCheckBox>
#include <QSpacerItem>
#include <QLineEdit>
#include <QGroupBox>
#include <QComboBox>
#include "browseimages.h"

BrowseImages::BrowseImages(QWidget *parent) : QWidget(parent)
{
// Title of the window
this->setWindowTitle("Image Browser");
this->resize(700, 400);

b_search = new QPushButton(tr("SEARCH"));

b_exit = new QPushButton(tr("Exit"));

combo = new QComboBox (this);

QHBoxLayout *h_exit = new QHBoxLayout;
h_exit->addStretch();
h_exit->addWidget(b_exit);
h_exit->addStretch();

connect(b_exit, SIGNAL(clicked()), qApp, SLOT(quit()));

l_welcome = new QLabel(tr("--Image Browser--"));
l_welcome->setFont(QFont("Times", 18, QFont::Bold));
l_welcome->setAlignment(Qt::AlignCenter);

g_graphics = new QGraphicsView(this);

t_box = new QGroupBox(this);
//t_box= new QGroupBox(tr("Tool Box"));
t_box->setFixedSize(150,270);


QHBoxLayout *v_g = new QHBoxLayout;
v_g->setAlignment(Qt::AlignCenter);
v_g->addWidget(g_graphics);
v_g->addWidget(t_box);

QHBoxLayout *v_e = new QHBoxLayout;
v_e->setAlignment(Qt::AlignRight);
v_e->addWidget(combo);

QHBoxLayout *v_m= new QHBoxLayout;
v_m->setAlignment(Qt::AlignRight);
v_m->addWidget(b_exit);

/*QHBoxLayout *v_w= new QHBoxLayout;
v_w->setAlignment(Qt::AlignRight);
v_w->addWidget(v_e);
v_w->addWidget(v_m);*/



//--------------------------------//
QHBoxLayout *sub1 = new QHBoxLayout;
sub1->setAlignment(Qt::AlignRight);
cb1= new QCheckBox(tr("Search By Plate Number"));
sub1->addWidget(cb1);

QHBoxLayout *sub2 = new QHBoxLayout;
sub2->setAlignment(Qt::AlignRight);
l1= new QLineEdit(this);
sub2->addWidget(l1);

QHBoxLayout *sub3 = new QHBoxLayout;
sub3->setAlignment(Qt::AlignLeft);
cb2= new QCheckBox(tr("Search By Date"));
sub3->addWidget(cb2);


QHBoxLayout *sub4 = new QHBoxLayout;
sub4->setAlignment(Qt::AlignLeft);
l2= new QLineEdit(this);
sub4->addWidget(l2);

QHBoxLayout *sub5 = new QHBoxLayout;
sub5->setAlignment(Qt::AlignCenter);
b_ok= new QPushButton(tr("OK"));
b_ok->setFixedHeight(20);
sub5->addWidget(b_ok);


QVBoxLayout *mainsub = new QVBoxLayout;
mainsub->setAlignment(Qt::AlignRight);
mainsub->addLayout(sub1);
mainsub->addLayout(sub2);
mainsub->addSpacing(40);
mainsub->addLayout(sub3);
mainsub->addLayout(sub4);
mainsub->addLayout(sub5);
t_box->setLayout(mainsub);

//------------------------------------------//


QVBoxLayout *layout_vert = new QVBoxLayout;
layout_vert->setAlignment(Qt::AlignCenter);
layout_vert->addWidget(l_welcome);
layout_vert->addLayout(v_g);
layout_vert->addLayout(v_e);
layout_vert->addSpacing(20);
layout_vert->addLayout(v_m);
this->setLayout(layout_vert);
}



Thank you very much.

jpn
20th October 2007, 18:20
It's simply painful to read and to try to understand complex and hand made layouting code. Seems you are able to do it with Qt Designer (even if it's missing layouts though) so may I ask why aren't you using it then? :D