Qt之自定义界面(二)添加最小化、关闭按钮、添加背景

港控/mmm° 2022-08-05 05:21 409阅读 0赞

Qt之自定义界面(二)添加最小化、关闭按钮、添加背景

此博文包含图片 (2013-01-15 19:05:22)

sg_trans.gif转载▼








标签: 

qt

 

自定义

 

最大化

 

关闭

 

背景

分类: 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;”);

这样我们的最小化、最大化按钮就已经出现!

效果如下:

Qt之自定义界面(二)添加最小化、关闭按钮、添加背景

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);

  1. QPixmap pixmap(":/icons/login");
  2. painter.drawPixmap(this->rect(), pixmap);

}

这里就不演示效果了,都没问题的!

注:

  1. 技术在于交流、沟通,转载请注明出处并保持作品的完整性。
  2. 作者: [╰☆奋斗ing❤孩子\`][ing] 原文: [http://blog.sina.com.cn/s/blog\_a6fb6cc90101auay.html][http_blog.sina.com.cn_s_blog_a6fb6cc90101auay.html]。

发表评论

表情:
评论列表 (有 0 条评论,409人围观)

还没有评论,来说两句吧...

相关阅读