本文主要是介绍Qt之自定义界面(二)添加最小化、关闭按钮、添加背景,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在进行自定义界面之后,Qt界面并不像我们想象的那么完美,也许与我们的预期大相径庭,但是不必心烦,这只不过是迈出了第一步而已,第一步既然都已经迈出去了,那么以后的路就会好走多了!
1、自定义最小化、最大化按钮
int width = width();//获取界面的宽度
//构建最小化、最大化、关闭按钮
QToolButton *minButton = new QToolButton(this);
QToolButton *closeButton= new QToolButton(this);
//获取最小化、关闭按钮图标
QPixmap minPix = style()->standardPixmap(QStyle::SP_TitleBarMinButton);
QPixmap closePix = style()->standardPixmap(QStyle::SP_TitleBarCloseButton);
//设置最小化、关闭按钮图标
minButton->setIcon(minPix);
closeButton->setIcon(closePix);
//设置最小化、关闭按钮在界面的位置
minButton->setGeometry(width-46,5,20,20);
closeButton->setGeometry(width-25,5,20,20);
//设置鼠标移至按钮上的提示信息
minButton->setToolTip(tr("最小化"));
closeButton->setToolTip(tr("关闭"));
//设置最小化、关闭按钮的样式
minButton->setStyleSheet("background-color:transparent;");
closeButton->setStyleSheet("background-color:transparent;");
这样我们的最小化、最大化按钮就已经出现!
效果如下:

2、自定义背景
自定义背景可以使用多种方法,在这里我就介绍三种
(1)使用 QLable,加载图片的方式
QLabel *background = new QLabel(this);
//设置标签的显示图片
background->setPixmap(QPixmap(":/icon/login"));
//设置背景图片的位置大小
background->setGeometry(0, 0, this->width(), this->height());
//设置图片充满整个标签
background->setScaledContents(true);
(2)使用 QLable,选择背景色的方式
QLabel *background = new QLabel(this);
//设置标签的背景色为蓝色
background->->setStyleSheet("background-color:blue");
//设置背景标签的位置大小
background->setGeometry(0, 0, this->width(), this->height());
(3)重写void paintEvent(QPaintEvent *)
void MainWidget::paintEvent(QPaintEvent *)
{
QBitmap bitmap(this->size());
bitmap.fill();
QPainter painter(&bitmap);
QPixmap pixmap(":/icons/login");
painter.drawPixmap(this->rect(), pixmap);
}
这里就不演示效果了,都没问题的!
注:
技术在于交流、沟通,转载请注明出处并保持作品的完整性。
作者: ╰☆奋斗ing❤孩子` 原文: http://blog.sina.com.cn/s/blog_a6fb6cc90101auay.html。
这篇关于Qt之自定义界面(二)添加最小化、关闭按钮、添加背景的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!