PDA

View Full Version : problem in layout.



Niamita
12th October 2011, 05:30
Hi everyone
i have a custom frame on which i draw some shapes with paintevent, when i adjust this custom frame widget in layout, it is not getting resized, means if i compress the window then some shape are hide , only some are displaying which are under the window size.
What i should done so that my custom frame willl also work in layout.

Waiting for response.
Thanks.

wysota
12th October 2011, 06:28
You have to respect the size of the frame when implementing the paint event. Your frame does respect a layout, it's just you don't respect the frame can be resized.

Niamita
12th October 2011, 06:53
Please be more elaborated.

wysota
12th October 2011, 06:57
You can't fit a 200x200 image into a 100x100 widget regardless how hard you try. The image will be cut and clipped to the size of the widget. If you want the image to always fit the widget, the widget has to inform its environment about the minimum size it may have for instance by implementing sizeHint() and minimumSizeHint().

Niamita
12th October 2011, 07:02
So i have to implement minimumSizeHint() or sizeHint() in custom frame and then fit it into layout. right?

wysota
12th October 2011, 07:31
It's a good start.

Niamita
12th October 2011, 07:43
I am implementing minimumSizeHint() in frame class but nothing is change in output.
i am doing like this.

QSize button::minimumSizeHint()
{
return QSize(1000,200);

}

Am i doing anything wrong.
After implementing this , do shapes will also expand or compressed with window in which i set this frame class in layout.

Please provide some sample of code to implement this.

wysota
12th October 2011, 07:55
I think you need to do some reading about how things work in Qt... There is no point in me rewriting half of the manual here.

Niamita
12th October 2011, 08:23
you are right but i implement this and this do not solve my problem. Do you understood exactly my problem.

wysota
12th October 2011, 09:19
I think you are falsely assuming that resizing a widget will cause content of the widget to be somehow squeezed to fit. Unless you implement it yourself, this is not going to happen. Or you're just using layouts incorrectly, hard to determine which because you didn't share any code.

Niamita
12th October 2011, 09:33
I have a custom frame class which is like this-


button::button(QWidget *parent) :
QMainWindow(parent){
resized = false;
desktop = QApplication::desktop();
screenwidth = desktop->width(); // get width of screen
screentheight = desktop->height(); // get height of screen
this->setGeometry(0, 0, screenwidth, screentheight);
this->setWindowTitle("Home Profile");

void button::paintEvent(QPaintEvent *event ){

QPainter painter_menu_button(this);
QPen pen_menu_button(QColor(10, 10, 10, 120));
painter_menu_button.setPen(pen_menu_button);
painter_menu_button.setRenderHint(QPainter::Antial iasing);
painter_menu_button.setBrush(QColor(255, 255, 255));

/************************************ Home Button **************************************/

static const QPointF points_home[4] = {
QPointF(Home_Profile_Q1_X, Home_Profile_Q1_Y),
QPointF(Home_Profile_Q2_X, Home_Profile_Q2_Y),
QPointF(Home_Profile_Q3_X, Home_Profile_Q3_Y),
QPointF(Home_Profile_Q4_X, Home_Profile_Q4_Y),
};
painter_menu_button.drawPolygon(points_home, 4);

/************************************ Emergency Button **************************************/

static const QPointF points_emergency[4] = {
QPointF(Emergency_Help_Q1_X, Emergency_Help_Q1_Y),
QPointF(Emergency_Help_Q2_X, Emergency_Help_Q2_Y),
QPointF(Emergency_Help_Q3_X, Emergency_Help_Q3_Y),
QPointF(Emergency_Help_Q4_X, Emergency_Help_Q4_Y),
};
painter_menu_button.drawPolygon(points_emergency, 4);

/************************************ Light Button **************************************/

static const QPointF points_light[4] = {
QPointF(Light_Q1_X, Light_Q1_Y),
QPointF(Light_Q2_X, Light_Q2_Y),
QPointF(Light_Q3_X, Light_Q3_Y),
QPointF(Light_Q4_X, Light_Q4_Y),
};
painter_menu_button.drawPolygon(points_light, 4);

/************************************ Video Button **************************************/

static const QPointF points_video[4] = {
QPointF(Video_Voor_Q1_X, Video_Voor_Q1_Y),
QPointF(Video_Voor_Q2_X, Video_Voor_Q2_Y),
QPointF(Video_Voor_Q3_X, Video_Voor_Q3_Y),
QPointF(Video_Voor_Q4_X, Video_Voor_Q4_Y),
};
painter_menu_button.drawPolygon(points_video, 4);

/************************************ Curtains Button **************************************/

static const QPointF points_curtains[4] = {
QPointF(Curtains_Q1_X, Curtains_Q1_Y),
QPointF(Curtains_Q2_X, Curtains_Q2_Y),
QPointF(Curtains_Q3_X, Curtains_Q3_Y),
QPointF(Curtains_Q4_X, Curtains_Q4_Y),
};
painter_menu_button.drawPolygon(points_curtains, 4);

/************************************** Custom Button **************************************/

static const QPointF points_custom[4] = {
QPointF(Custom_Comfort_Q1_X, Custom_Comfort_Q1_Y),
QPointF(Custom_Comfort_Q2_X, Custom_Comfort_Q2_Y),
QPointF(Custom_Comfort_Q3_X, Custom_Comfort_Q3_Y),
QPointF(Custom_Comfort_Q4_X, Custom_Comfort_Q4_Y),
};
painter_menu_button.drawPolygon(points_custom, 4);

/************************************* Button Text Drawing ***********************************/

QPainter painter_text(this);
QPen pen_text(QColor(189, 127, 66, 255));
painter_text.setPen(pen_text);
if((screenwidth >= 800) && (screenwidth <= 1024)){
QFont font("Verdana", 11);
painter_text.setFont(font);
}
if((screenwidth > 1024) && (screenwidth <= 1366)) {
QFont font("Verdana", 14);
painter_text.setFont(font);
}
QRect button1_text_1stline = QRect(Text_Home_X, Text_Home_Y, screenwidth/13, screentheight/30);
painter_text.drawText( button1_text_1stline, Qt::AlignLeft, "Home");
QRect button1_text_2ndtline = QRect(Text_Profile_X, Text_Profile_Y, screenwidth/13, screentheight/30);
painter_text.drawText( button1_text_2ndtline, Qt::AlignLeft, "Profile");
QRect button2_text_1stline = QRect(Text_Emergency_X, Text_Emergency_Y, screenwidth/13, screentheight/30);
painter_text.drawText( button2_text_1stline, Qt::AlignLeft, "Emerency");
QRect button2_text_2ndline = QRect(Text_Help_X, Text_Help_Y, screenwidth/13, screentheight/30);
painter_text.drawText( button2_text_2ndline, Qt::AlignCenter, "Help");
QRect button3_text1stline = QRect(Text_Light_X, Text_Light_Y, screenwidth/13, screentheight/30);
painter_text.drawText( button3_text1stline, Qt::AlignCenter, "Lights");
QRect button4_text1stline = QRect(Text_Video_Door_X, Text_Video_Door_Y, screenwidth/12, screentheight/30);
painter_text.drawText( button4_text1stline, Qt::AlignCenter, "Video Door");
QRect button4_text2ndline = QRect(Text_Phone_X, Text_Phone_Y, screenwidth/13, screentheight/30);
painter_text.drawText( button4_text2ndline, Qt::AlignCenter, "Phone");
QRect button5_text1stline = QRect(Text_Curtains_X, Text_Curtains_Y, screenwidth/13, screentheight/30);
painter_text.drawText( button5_text1stline, Qt::AlignCenter, "Curtains");
QRect button6_text1stline = QRect(Text_Custom_X, Text_Custom_Y, screenwidth/13, screentheight/30);
painter_text.drawText( button6_text1stline, Qt::AlignLeft, "Custom");
QRect button6_text2ndline = QRect(Text_Comfort_X, Text_Comfort_Y, screenwidth/13, screentheight/30);
painter_text.drawText( button6_text2ndline, Qt::AlignLeft, "Comfort");
}

above is a frame have buttons drawn by paint.

Now my main class in which i use this frame in a layout


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)//,
//ui(new Ui::MainWindow)
{
//ui->setupUi(this);

QDesktopWidget *desktop = QApplication::desktop();
screenWidth = desktop->width(); // get width of screen
screenHeight = desktop->height(); // get height of screen
this->setGeometry(1,30,screenWidth,screenHeight);
button_on = new QPushButton(this);
button_on->setText("ON");
button_on->setMaximumSize(Button_1st_Width, Button_1st_Height);
//button_on->setGeometry(Button_On_X, (screenHeight/2) - screenHeight/20, Button_On_Width, Button_On_Height);

button_off = new QPushButton(this);
button_off->setText("OFF");
button_off->setMaximumSize(Button_1st_Width, Button_1st_Height);
//button_off->setGeometry(Button_Off_X, (screenHeight/2) - screenHeight/20, Button_Off_Width, Button_Off_Height);

button_1st = new QPushButton(this);
//button_1st->setGeometry(Button_1st_X, screenHeight/4 - (screenHeight/10), Button_1st_Width, Button_1st_Height);
button1 = 75;
button_1st->setMaximumSize(Button_1st_Width, Button_1st_Height);
button_1st->setText(QString::number(button1) + "%");

button_2nd = new QPushButton(this);
//button_2nd->setGeometry(Button_2nd_X, (screenHeight/4 - (screenHeight/10)) + screenHeight/8, Button_2nd_Width, Button_2nd_Height);
button2 = 50;
button_2nd->setMaximumSize(Button_1st_Width, Button_1st_Height);
button_2nd->setText(QString::number(button2) + "%");

button_3rd = new QPushButton(this);
//button_3rd->setGeometry(Button_3rd_X, (screenHeight/4 - (screenHeight/10)) + screenHeight/8 + screenHeight/8, Button_3rd_Width, Button_3rd_Height);
button3 = 25;
button_3rd->setText(QString::number(button3) + "%");
button_3rd->setMaximumSize(Button_1st_Width, Button_1st_Height);

button_up_arrow = new QPushButton(this);
button_up_arrow->setMaximumSize(Button_1st_Width, Button_1st_Height);
//button_up_arrow->setGeometry(Button_up_arrow_X, (screenHeight/4 - (screenHeight/10)) + screenHeight/15, Button_up_arrow_Width, Button_up_arrow_Height);

button_down_arrow = new QPushButton(this);
button_down_arrow->setMaximumSize(Button_1st_Width, Button_1st_Height);
//button_down_arrow->setGeometry(Button_down_arrow_X, (screenHeight/4 - (screenHeight/10)) + screenHeight/5, Button_down_arrow_Width, Button_down_arrow_Height);

label_light_name = new QLabel(this);
label_light_name->setText("Gym Light");
label_light_name->setMaximumSize(Button_1st_Width, Button_1st_Height);
//label_light_name->setGeometry(screenWidth/3 + screenWidth/12, screenHeight/50, Button_1st_Width, Button_1st_Height);

QFrame *frame = new QFrame(this);
this->setCentralWidget(frame);

button *btn = new button(this);
//var = btn->minimumSizeHint();
//qDebug()<<var;
QGridLayout *grid = new QGridLayout(this);
grid->setVerticalSpacing(20);
grid->setHorizontalSpacing(40);
grid->addWidget(btn, 0,0);
grid->addWidget(label_light_name, 0,2,1,4);
grid->addWidget(button_1st, 1,1);
grid->addWidget(button_2nd,2,1);
grid->addWidget(button_3rd,3,1);
grid->addWidget(button_up_arrow, 1,2,2,1);
grid->addWidget(button_down_arrow, 2,2,2,1);
grid->addWidget(button_on,4,0);
grid->addWidget(button_off,4,3);
// grid->setColumnStretch(0,60);
// grid->setColumnStretch(1,500);
frame->setLayout(grid);

}

Problem is that when i resizing the window all widget autofit to window but shape drawn over frame does not fit.
I have to autofit shapes also.
Please guide me.

wysota
12th October 2011, 09:42
Your button class is derived from QMainWindow? Are you sure this is a good idea? Furthermore despite what I told you, you never check the size of the widget in your paint event. I don't see how you thought this would work... You only check desktop size which is completely irrelevant to the widget size. You just keep drawing "somewhere". Close your eyes, I will put a piece of paper on your wall and then you'll start drawing with your eyes closed without knowing where the paper is and how big it is. There is a good chance you'll have to repaint your wall afterwards...

Niamita
12th October 2011, 09:59
Ohh
This is than a big problem. Now either i pass width and height of widget or any other way i have to do this.

wysota
12th October 2011, 11:17
Man... did you at least have a look at the manual for QWidget and some custom widget examples?