Qt 各种控件 设置背景(持续更新)

来源:互联网 发布:校园招聘 云计算 编辑:程序博客网 时间:2024/06/16 09:59

父窗口widget:

    QPixmap pixmap(":/iphoneUi/background.png");
    palette.setBrush(backgroundRole(), QBrush(pixmap));
    this->setPalette(palette);
    this->setAutoFillBackground(true);


用法跟stackedwidget完全一致,下面介绍。



listwidget:

    roomList = new QListWidget;
    palette.setBrush(QPalette::Base, QBrush(QPixmap(":/iphoneUi/roomlistbackground2.png")));
    roomList->setPalette(palette);
    roomList->setFrameShape(QFrame::NoFrame);
    roomList->setMinimumSize(660,130);
    roomList->setMaximumSize(660,130);
    roomList->setIconSize(QSize(100,100));
    roomList->setViewMode(QListView::IconMode);
    roomList->setMovement(QListView::Static);
  //  roomList->setStyleSheet("background:transparent");
   // roomList->setAutoFillBackground(true);
 


listwidget 本身背景是白色,如果敲上oomList->setStyleSheet("background:transparent");可以与父窗口背景一致

这里用palette.setBrush刷子刷,QPalette::Base,qpixmap载入背景图片

记得setpalette。

setautofillbackground不能钩。


stackedwidget:

    labelStackedWidget = new QStackedWidget;
    QPixmap pixmapl(":/iphoneUi/somkedbackground.png");
    palette.setBrush(backgroundRole(), QBrush(pixmapl));
    labelStackedWidget->setPalette(palette);
    labelStackedWidget->setFrameShape(QFrame::NoFrame);
    labelStackedWidget->setFrameShadow(QFrame::Plain);
    labelStackedWidget->setLineWidth(1);
    labelStackedWidget->setMidLineWidth(0);
    labelStackedWidget->setMinimumSize(500,60);
    labelStackedWidget->setMaximumSize(500,60);
    labelStackedWidget->setAutoFillBackground(true);


一样用palette。setbrush和pixmap。但是backgroundrole不同。

一样记得setpalette

注意。这里一定要勾上setAutoFillBackground(true);。不然背景就跟父窗口一致。



label:

最简单一句代码!

label->setPixmap(QPixmap(":/lightproject/timing.png"));

原创粉丝点击