经实验,该种设置背景的方法最为有效。如下所示,this指代要设置背景的当前控件,ButtonBar指代当前控件所属的类。
this->setStyleSheet("ButtomBar{border-image: url(:/image/img_Background_bottom1.png);}");
this->setStyleSheet("ButtomBar {background-color: black}");
void ButtomBar::paintEvent(QPaintEvent *) //继承QWidget的子类需要这个函数才能设置样式表,非必需
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
另外,对于大量按钮需求的布局方式,选择绝对定位(move)方法最为简洁有效,且代码结构清晰完整。
其中要注意的是, 使用资源图片时需要将操作图片加入到分当前项目的资源之中,具体如下
右键点击Qt Resources File新建
右键open in editor编辑
先添加前缀并改成/
添加文件之后调用即可
另外可参考其他QT相关博客中添加背景的方法:http://blog.csdn.net/itjobtxq/article/details/9284415