2011-08-24 57 views
1

我已经在Qt中创建了一个无窗口窗口,它具有小部件和背景。但是我对这种形式的一个问题,当我调整表单中的所有控件调整好,但如果没有大小调整发生的背景不 示范在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 

回答

1

由于您的控件居中在窗口中,但看起来不像它们,因此可能表示在用作背景的图像的非透明部分周围存在透明边框。

您可以从刷子除去透明度paintEvent确认,有例如:

painter.setBrush(QColor(0, 0, 0, 255)); 

更清晰,问题是不是在你的代码,但图像在:用编辑器打开图像,只选择非透明部分,通过使用“裁剪工具”仅保留该部分,最后保存图像。

+0

用Code可以解释一下吗? – Legend

+0

现在更清楚了吗? – alexisdm

+0

非常感谢,但没有必要更换画笔。 – Legend