2011-08-24 55 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