Resizing a label inside a gridlayout?
I'm learning Qt, maybe this is easy but I can't seem to figure it out.
First, I have a file path in a QString, myPath
. I开发者_Python百科 want to load it and show it in a window.
Suppose I have a window with a label inside it:
QPixmap image(myPath);
ui->myLabel->setPixmap(image);
ui->myLabel->resize(image.size());
It works, but the on the right there is no border. I thought about fixing that with a gridlayout. I'm not sure if that's the appropriate solution however.
Anyway, that means I'd have a gridlayout in the window and then the label inside. But once I use that pice of code, the image is only as big as the gridlayout.
So, how can I have my image with some borders around it, and how can I have the gridlayout resize itself appropriately?
I'm not sure if I understood correctly but I believe you want some padding inside the label. That can be accomplished for example with QSS, Qt's flavor of CSS.
ui->myLabel->setStyleSheet("QLabel { padding: 10px; }");
should do the trick.
精彩评论