Rud*_*eri 2 c++ qt background-image qpixmap qpushbutton
我需要将内部资源中的图像作为QPushButton的背景图像.我还需要调整图像的大小到按钮的大小.我在这里找到了一些方法来做到这一点,但似乎没有任何工作.在上一个回复中,建议使用以下代码:
QPixmap pixmap("image.jpg");
QPalette palette;
QPushButton *button= new QPushButton(this);
palette.setBrush(button->backgroundRole(), QBrush(pixmap));
button->setFlat(true);
button->setAutoFillBackground(true);
button->setPalette(palette);
Run Code Online (Sandbox Code Playgroud)
所以我拿了那个代码并稍微改了一下因为我使用的是用QTCreator制作的ui:
void MyDialog::SetBgImage(QWidget *pButton)
{
QPixmap pixmap(":/Icons/images/Sfondo.png");
QPalette palette = pButton->palette();
palette.setBrush(pButton->backgroundRole(), QBrush(pixmap)); // 1
QPushButton *pPButton = qobject_cast<QPushButton *>(pButton);
if (pPButton!=NULL)
pPButton->setFlat(true);
pButton->setAutoFillBackground(true);
pButton->setPalette(palette); // 2
Run Code Online (Sandbox Code Playgroud)
}
在构造函数中,我以这种方式调用它:
SetBgImage(ui->pushButton_Button1);
Run Code Online (Sandbox Code Playgroud)
显示对话框时,按钮显示正确.不幸的是,当我关闭对话框时,我收到以下错误消息:
*检测到glibc* ./MyAppName:malloc():内存损坏:0x0047dc78***
如果我删除标有// 1的行或标有// 2的行,则错误消失.
有任何想法吗?
button->setStyleSheet("border-image:url(:/Icons/images/Sfondo.png);");
Run Code Online (Sandbox Code Playgroud)
或者从设计师更改样式表.
| 归档时间: |
|
| 查看次数: |
10586 次 |
| 最近记录: |