很多时候根据不同的需要 要将界面根据图片做成各种各样的形状的界面
而界面不能和控件一样 直接用png图做border 无法实现 仍旧是方方正正的
如何制作自义定的窗口界面呢?
这要用到QPixmap 图像映射
首先在建立窗口时
Dialog *w = new Dialog;
w->setWindowFlags(Qt::FramelessWindowHint);//去边框
w->setAttribute(Qt::WA_TranslucentBackground);//背景透明
w->show();
首先在头文件类中声明
private:
QPixmap* backgroundPixmap_;
之后重写 paintEvent
void Dialog::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.fillRect(0, 0, backgroundPixmap_->width(), backgroundPixmap_->height(), *backgroundPixmap_);
}
然后进行映射
backgroundPixmap_ = new QPixmap(":/new/prefix1/image/1.png"); //映射资源类图片
this->resize(backgroundPixmap_->width(), backgroundPixmap_->height());
this->clearMask();//清除原来的图片信息
this->setMask(backgroundPixmap_->mask());
this->update();//更新显示
编译 运行 完成
这样 就可以将窗口根据图片来进行设置了