Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Which Step do I missed? About Frameless QWidget
QtWS25 Last Chance

Which Step do I missed? About Frameless QWidget

Scheduled Pinned Locked Moved Solved General and Desktop
framelesswindow
4 Posts 2 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Manifolds
    wrote on 20 May 2017, 10:38 last edited by Manifolds
    #1
    • 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
    
    
    • framelesswindow.cc
    #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
    
    
    • Widget.cc
    #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.

    1 Reply Last reply
    0
    • M Manifolds
      20 May 2017, 15:32

      @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.

      M Offline
      M Offline
      Manifolds
      wrote on 21 May 2017, 14:03 last edited by
      #4

      @Manifolds

      SOLVED

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mpergand
        wrote on 20 May 2017, 14:15 last edited by
        #2

        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);
        }
        
        M 1 Reply Last reply 20 May 2017, 15:32
        0
        • M mpergand
          20 May 2017, 14:15

          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);
          }
          
          M Offline
          M Offline
          Manifolds
          wrote on 20 May 2017, 15:32 last edited by
          #3

          @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.

          M 1 Reply Last reply 21 May 2017, 14:03
          0
          • M Manifolds
            20 May 2017, 15:32

            @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.

            M Offline
            M Offline
            Manifolds
            wrote on 21 May 2017, 14:03 last edited by
            #4

            @Manifolds

            SOLVED

            1 Reply Last reply
            0

            1/4

            20 May 2017, 10:38

            • Login

            • Login or register to search.
            1 out of 4
            • First post
              1/4
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved