PDA

View Full Version : resizeEvent - resizing unproportionally



Tomasz
12th August 2010, 12:40
Hello!

I've wrote my widget (which is something like plot). When I put it in layout it works fine. Widget is square (height and width is the same). When layout isn't square (for example width is longer than height) widget stays squared (height=width). And when layout is bigger then widget it stays in size that it was drawn (only when layout is smaller then widget it goes smaller). Can I change it somehow? Or is it some kind of limitation? My resizeEvent code:



void WykresSlupkowy::resizeEvent(QResizeEvent *event)
{
QWidget::resizeEvent(event);
}


thanks in advance
best regards
Tomasz

kremuwa
12th August 2010, 12:54
You didn't tell us how do you want it exactly to behave. So far your reimplemented resizeEvent() seems to do nothing but calling a standard QWidget resizeEvent, it might not be there as well and the effect would be the same.

BTW: Cześć ;).

Tomasz
12th August 2010, 15:13
So I want my widget to be same size as layout (for example QHBoxLayout). If i set it for example:



horizontalLayoutWidget->setGeometry(QRect(QPoint(100,100),QPoint(300,300)) );


I want my widget to resize to that dimensions. And one more thing, I've got in my code some lines that I don't understand (get it from some program):



int side = qMin(width(), height());

QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.scale(side / 256.0, side / 256.0);


I think it has something with resize. Maybe I should change something here?

thanks in advance
best regards
Tomasz

Urthas
13th August 2010, 22:50
I think you're mixing things up here. It sounds like you want to resize your widget, but resizeEvent() is what get's called when the widget is resized, not what you call to do the resizing (since that would cause infinite recursion). Does the following basically sum up the logic of what you want to do?



QRect r = yourLayout.geometry();
yourWidget.resize(r.width(), r.height());


With regards to the painter code that you mention, it says the following in QWidget's resizeEvent() documentation: "The widget will be erased and receive a paint event immediately after processing the resize event. No drawing need be (or should be) done inside this handler."

I apologize if this isn't helpful or, God forbid, leads you further astray. As a newbie perhaps I should just keep my mouth shut but maybe someone will correct me and I'll learn too. :P

edit: By the way, why ARE you trying to resize a widget based on the layout? Usually a widget is resized by the user directly (as in dragging a window's borders around) or by the layout manager in response to such direct action, or as more widgets are added to the layout. The fact that layouts don't have size/resize/resizeEvent methods is not accidental. I think that most of the time you're not supposed to be concerned with a layout's size...

Lykurg
13th August 2010, 23:22
I've got in my code some lines that I don't understand (get it from some program):



int side = qMin(width(), height());

QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.scale(side / 256.0, side / 256.0);


I think it has something with resize. Maybe I should change something here?
:D Yes, because that is one possible reason why your widget always show as a square. Before you copy without thinking, and then waste your time by dealing with (nonsense) layout - widget relation and try there dirty hacks, you should spend some time to understand the copied code.
By seeing the whole paint event your problem could be clearer for us.

Tomasz
15th August 2010, 19:55
Does the following basically sum up the logic of what you want to do?


QRect r = yourLayout.geometry();
yourWidget.resize(r.width(), r.height());



Ok. @Lykurg is right - I should use code that I dont't understand. I've read class reference about it. I think it's not what I'm looking for. I've tried code above but it doesn't work. My widget looks like this:



WykreSlupkowy::WykresSlupkowy(*QWidget parent) : QWidget(parent)
{
}

void WykresSlupkowy::paintEvent(QPaintEvent *event)
{
paintMyWidget();
QWidget::paintEvent(event);
}

void WykresSlupkowy::resizeEvent(QResizeEvent *event)
{
QWidget::reziseEvent(event);
}


So what should I do to get size I want?

thanks in advance
best regards
Tomasz

Lykurg
15th August 2010, 20:19
The code does not help much. We need to see how you make your drawing. But before: Your widget stays a square or your drawing?

to determine you can use:
void WykresSlupkowy::paintEvent(QPaintEvent *event)
{
paintMyWidget();
QWidget::paintEvent(event);
QPainter p(this);
p.setPen(QPen(Qt::red, 1));
p.drawRect(rect().adjusted(0, 0, -1, -1));
}

So is the red border always a square?

Tomasz
16th August 2010, 23:52
Without scaling thing my widget stays the size I drew it. Code posted by @Lykurg draws rectangle of my layout size like I want. Code of my widget looks like this:



#include "slupkowy.h"

WykresSlupkowy::WykresSlupkowy(QWidget *parent) : QWidget(parent)
{

}

void WykresSlupkowy::paintEvent(QPaintEvent *event)
{
narysujWykres();
QWidget::paintEvent(event);
QPainter p(this);
p.setPen(QPen(Qt::red, 1));
p.drawRect(rect().adjusted(0, 0, -1, -1));
}

void WykresSlupkowy::resizeEvent(QResizeEvent *event)
{
QWidget::resizeEvent(event);
}

void WykresSlupkowy::ustawNazwePliku(QString nazwa)
{
nazwaPlikuDanych = nazwa;
}

void WykresSlupkowy::odczytajDane()
{
QFile plikDanych(nazwaPlikuDanych);
QString line;
bool ok;
int nrDanej = 0;

if (plikDanych.open(QFile::ReadOnly))
{
QTextStream in(&plikDanych);
QTextStream out(stdout);
while ( !in.atEnd() )
{
line=in.readLine();
out << line << " ";
dane[nrDanej]=line.toInt(&ok,10);
nrDanej++;
}

}
}

void WykresSlupkowy::rysujOsie()
{
int side = qMin(width(), height());
int wykresX, wykresY;

QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
//painter.scale(256.0, side / 256.0);

painter.setPen(QColor("#000000"));
painter.setBrush(QColor("#000000"));

//os X
painter.drawLine(QPoint(14,151),QPoint(234,151));
wykresX=20;
for(int i=0; i<24; i++)
{
painter.drawLine(QPoint(wykresX+(i*9),149),QPoint( wykresX+(i*9),153));
}

//os Y
painter.drawLine(QPoint(14,151),QPoint(14,0));
wykresY=151;
for(int i=1; i<15; i++)
{
painter.drawLine(QPoint(12,wykresY-(i*10)),QPoint(16,wykresY-(i*10)));
}

//groty strzalek
QPoint strzalka[3] = {
QPoint(14,0),
QPoint(12,5),
QPoint(16,5)
};

painter.drawPolygon(strzalka, 3);

strzalka[0]=QPoint(234,151);
strzalka[1]=QPoint(229,149);
strzalka[2]=QPoint(229,153);

painter.drawPolygon(strzalka, 3);

//liczby na osiach
QFont serifFont( "Times", 4);
painter.setFont(serifFont);

for(int i=1; i<15; i++)
{
painter.drawText(0,154-(i*10),QString::number(i*10));
}

for(int i=1; i<25; i++)
{
painter.drawText(18+((i-1)*9),165,QString::number(i));
}
}

void WykresSlupkowy::narysujWykres()
{
int side = qMin(width(), height());

QTextStream out(stdout);
out << side ;
for(int i=0; i<24; i++)
{
dane[i]=i+30+(i*2);
}

QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
//painter.scale(side / 256.0, side / 256.0);
painter.setPen(QColor("#ffffff"));
painter.setBrush(QColor("#ff0000"));
painter.setBrush(Qt::SolidPattern);

int wykresX=15;;

for(int i=0; i<24; i++)
{
painter.setBrush(QColor(255,0,0));
painter.drawRect(QRect(QPoint(wykresX,150),QPoint( wykresX+8,150-dane[i])));
wykresX+=9;
}

rysujOsie();
}


It paints simple plot. Data is from text file. Only WykresSlupkowy::narysujWykres() and WykresSlupkowy::rysujOsie() paints something. Any suggestions how should I write my widget to resize it how i want?

thanks in advance
best regards
Tomasz