我已经在Qt中创建了一个无框架的窗口,它有小部件和背景。但是我在表单中有一个问题,当我调整表单大小时,所有的小部件都调整得很好,但是背景没有看到这张图片来演示
当未调整大小时:
http://0000.2.img98.net/out.php/i20624_no-resize.jpg
调整大小时:
http://0000.2.img98.net/out.php/i20625_with-resize.jpg
下面是我创建表单的代码:
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QPushButton>
#include <QLabel>
#include <QComboBox>
#include <QPixmap>
#include <QVBoxLayout>
#include <QPainter>
#include <QMouseEvent>
#include <QtGui>
#include <QSizeGrip>
class MyWidget : public QWidget {
Q_OBJECT
private:
QPushButton* button;
QLabel* label;
QComboBox* combobox;
QPixmap pixmap;
public:
explicit MyWidget(QWidget *parent = 0) : QWidget(parent, Qt::FramelessWindowHint)
{
// Create some controls
button = new QPushButton();
label = new QLabel();
combobox = new QComboBox();
QVBoxLayout* l = new QVBoxLayout();
l->addWidget(button);
l->addWidget(label);
l->addWidget(combobox);
QSizeGrip *grip = new QSizeGrip(parent);
l->addWidget(grip, 0, Qt::AlignBottom | Qt::AlignRight);
setLayout(l);
resize (400, 500);
setAttribute(Qt::WA_TranslucentBackground); // enable translucent background
pixmap = QPixmap("./1.png");
}
protected:
virtual void paintEvent (QPaintEvent* event) {
QPainter painter(this);
painter.setPen(Qt::NoPen);
painter.setBrush(QColor(0, 0, 0, 0));
QRect rec = pixmap.rect();
painter.drawRect(this->rect());
painter.drawPixmap(this->rect(), pixmap, rec);
}
private:
bool pressed;
QPoint mousePressPoint;
protected:
virtual void mousePressEvent ( QMouseEvent * event ) {
QWidget::mousePressEvent(event);
if (!pressed) {
pressed = true;
mousePressPoint = event->pos();
}
}
#endif // MYWIDGET_H发布于 2011-08-24 18:09:22
由于您的控件在窗口中居中,但看起来并不是这样,这可能表明在用作背景的图像的非透明部分周围有一个透明的边框。
您可以在paintEvent中删除画笔的透明度以确认,例如:
painter.setBrush(QColor(0, 0, 0, 255));更清楚的是,问题不在你的代码中,而是在图片中:用编辑器打开图片,只选择不透明的部分,使用“裁剪工具”只保留该部分,最后保存图片。
https://stackoverflow.com/questions/7172726
复制相似问题