下一个项目要用qt来开发,看了几天的文档。
来写一篇给对话框增加背景的一个小例子。
来看一种添加.bmp的图片。
新建一个对话框程序。继承CDialogl.
classDialog:publicQDialog
{
Q_OBJECT
public:
explicitDialog(QWidget*parent=0);
~Dialog();
private:
Ui::Dialog*ui;
};
我也是最近才自学,有什么写的不对的地方,还请指出。谢谢。
说一下,qt5.1已经不支持。
#include<QtGui>
这句话啦。
我来先来定义一个私有的成员变量。QImage _image;
再来定义一个受保护的方法。
protected
void resizeEvent(QResizeEvent * event);
好了准备工作已经都做好啦。
好了,我们先来写
下来我们往resizeEvent 这个方法里写东西。
voidLaminator::resizeEvent(QResizeEvent*event)
{
QWidget::resizeEvent(event);
QPalettepal(palette());
pal.setBrush(QPalette::Window,QBrush(_image.scaled(event->size(),Qt::IgnoreAspectRatio,Qt::SmoothTransformation)));
setPalette(pal);
}
下来再写看一种方法。看看这个效果怎么样。
是在paint里写。
voidpaintEvent(QPaintEvent*);
定义这个方法。
voidDialog::paintEvent(QPaintEvent*)
{
QPainterpainter(this);
painter.drawPixmap(0,0,400,700,QPixmap("E:/psbe.png"));
或者写成这样
painter.drawPixmap(this->rect(),QPixmap("E:/psbe.png"));
}
这样写的化图片就不可以随着窗口的大小而改变啦。
还有一促是用样式表来完成。