vendredi 20 février 2015

QLabel keeps expanding on update.


Vote count:

0




I work on a program illustrating the shannon-fano-coding. I use a QLabel to show an image of the binary tree that makes up the coding (third tab on the bottom named "Baum"). My Problem is, whenever I update the image it expands 1px in height.


mainwindow.ui:



<layout class="QVBoxLayout" name="verticalLayout_9">
<item>
<widget class="QLabel" name="treeView">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
</layout>


mainwindow.cpp ctor:



MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
codec = new SFCodec(ui->inputField);
QWidget::showMaximized();

QImage temp(ui->treeView->width(), ui->treeView->height(), QImage::Format_ARGB32);
temp.fill(QColor(255,255,255));
ui->treeView->setPixmap(QPixmap::fromImage(temp));
ui->treeView->show();

}


mainwindow.cpp function call:



ui->treeView->setPixmap(QPixmap::fromImage(codec->getTreeView(ui->treeView->width(), ui->treeView->height())));


SFCodec.cpp drawing function:



QImage SFCodec::getTreeView(int width, int height)
{
qDebug() << width << " " << height;
int treeWidth = width-50, //the tree should have 25px spacing on each side
treeHeight = height-50,
max_step_x = treeWidth/4, //max step length (i.e. the first step big step from the root)
step_x = max_step_x, //the current step length in x-direction
step_y = 0,
depth = 0, //max depth of the tree
length = 0; //helper variable to calculate the depth

QPoint p1(treeWidth/2,5),p2(0,0); //Lines are drawn between two points. These are the two points...
QImage image(width, height, QImage::Format_ARGB32); //new image with the right dimensions

image.fill(QColor(255,255,255,255)); //filled in with white (NOTE: the format is ARGB so the first '255' is the alpha channel)
if(index.isEmpty()) //no input text -> no code -> no tree
return image;

QPainter painter(&image); //Qt device that does all the painting

painter.setPen(QPen(QColor(0,0,0))); //paints in black
painter.setBrush(QBrush(QColor(Qt::color0), Qt::NoBrush));
painter.setRenderHint(QPainter::Antialiasing); //activate antialiasing since performance isn't an issue

//calculate depth by finding the symbol with the longest code (each digit in the code is one level of depth)
std::for_each(index.begin(), index.end(),[&](Symbol sym)
{
length = sym.getCode().length();
if(length > depth)
depth = length;
});

//we have treeHeight many pixels and want to fill them with depth many levels. so the step length in y direction has to be this long:
step_y = treeHeight/depth;


//paint the path for each symbol
//note that we always start from the root so some lines are painted multiple times
//this could be prevented by a recursive call which would be less readable and
//(probably) not significantly faster.
std::for_each(index.begin(), index.end(),[&](Symbol sym)
{
p1 = QPoint(treeWidth/2,5); //reset to starting position
step_x = max_step_x; //and step length
QString temp = sym.getCode();
do{
if(temp.at(0) == '1')
p2 = p1 + QPoint(step_x,step_y);
else
p2 = p1 + QPoint(-step_x,step_y);
step_x = step_x/2;

painter.drawLine(p1,p2);
p1 = p2;

temp.remove(0,1);
}while(temp.size());
p1 = p1 + QPoint(-5,15);
painter.drawText(p1, sym.getSym());
});
painter.end();
return image;
}


I'm pretty sure it has something to do with the layout since my code always uses the label's width() and height() functions to get the values and I don't change anything by one anyway.


I know there are thousands of threads about layouts and resizing on here and I looked through quite a few of them but non had a widget expanding on it's own or anything similar.


If you want to contribute anything to the project (it's for a computer science lecture at my university) feel invited to do so on the corresponding github page. I'm an electrical engineer so my coding is naturally not up to standard.



asked 55 secs ago







QLabel keeps expanding on update.

Aucun commentaire:

Enregistrer un commentaire