Which Step do I missed? About Frameless QWidget



    • framelesswindows.hh
    #ifndef FRAMELESSWINDOW_HH
    #define FRAMELESSWINDOW_HH
    
    #include <QWidget>
    
    struct FramelessWindowPrivate;
    class FramelessWindow : public QWidget
    {
    public:
        explicit FramelessWindow(QWidget *contentWidget, QWidget *parent = 0);
        ~FramelessWindow();
    private:
        FramelessWindowPrivate *d;
    };
    
    #endif // FRAMELESSWINDOW_HH
    
    
    #include "framelesswindow.hh"
    
    #include <QMouseEvent>
    #include <QVBoxLayout>
    #include <QGraphicsDropShadowEffect>
    
    
    
    struct FramelessWindowPrivate {
        FramelessWindowPrivate(QWidget *contentWidget) : contentWidget(contentWidget) {}
        QWidget *contentWidget;
    };
    
    FramelessWindow::FramelessWindow(QWidget *contentWidget, QWidget *parent)
        : QWidget(parent)
    {
        this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
        this->setAttribute(Qt::WA_TranslucentBackground);
    
        d = new FramelessWindowPrivate(contentWidget);
        QVBoxLayout *lo = new QVBoxLayout(this);
        lo->addWidget(contentWidget);
    }
    FramelessWindow::~FramelessWindow() {
        delete d;
    }
    
    
    • Widget.hh
    #ifndef WIDGET_HH
    #define WIDGET_HH
    
    #include <QWidget>
    
    namespace Ui {
    class Widget;
    }
    
    class Widget : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit Widget(QWidget *parent = 0);
        ~Widget();
    
    private:
        Ui::Widget *ui;
    };
    
    #endif // WIDGET_HH
    
    
    #include "widget.hh"
    #include "ui_widget.h"
    
    Widget::Widget(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Widget)
    {
        ui->setupUi(this);
    }
    
    Widget::~Widget()
    {
        delete ui;
    }
    
    • Widget.ui
    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>Widget</class>
     <widget class="QWidget" name="Widget">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>400</width>
        <height>300</height>
       </rect>
      </property>
      <property name="focusPolicy">
       <enum>Qt::ClickFocus</enum>
      </property>
      <property name="windowTitle">
       <string>Widget</string>
      </property>
      <property name="autoFillBackground">
       <bool>false</bool>
      </property>
      <layout class="QVBoxLayout" name="verticalLayout_3">
       <item>
        <layout class="QVBoxLayout" name="verticalLayout_2">
         <item>
          <widget class="QPushButton" name="pushButton">
           <property name="text">
            <string>PushButton</string>
           </property>
          </widget>
         </item>
         <item>
          <widget class="QLabel" name="label">
           <property name="text">
            <string>TextLabel</string>
           </property>
          </widget>
         </item>
         <item>
          <spacer name="verticalSpacer">
           <property name="orientation">
            <enum>Qt::Vertical</enum>
           </property>
           <property name="sizeHint" stdset="0">
            <size>
             <width>20</width>
             <height>40</height>
            </size>
           </property>
          </spacer>
         </item>
         <item>
          <widget class="QLabel" name="label_2">
           <property name="text">
            <string>TextLabel</string>
           </property>
          </widget>
         </item>
        </layout>
       </item>
      </layout>
     </widget>
     <layoutdefault spacing="6" margin="11"/>
     <resources/>
     <connections/>
    </ui>
    

    The button, labels are seems not in the same Widget, and I can't click the QSpacerItem region. If open the autoFillBackground option, the widget will not have transparency.

    I want to know which step I have missed.



  • Maybe you need to draw a border yourself ...
    Something like that:

    void FramelessWindow::paintEvent(QPaintEvent *)
    {
    QPainter painter(this);
    QPainterPath path;
    path.addRoundedRect(contentsRect(), 5,5);
    QColor backgroundColor=palette.color(QPalette::Window);
    painter.fillPath(path,backgroundColor);
    }
    


  • @mpergand

    Yes, It does work.

    But If I have QWidget that is not the top-level, then the background remains when I close the widget.



  • @Manifolds

    SOLVED


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.