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. QGridLayout does not stretch the homemade QWidget over the workspace.
Forum Updated to NodeBB v4.3 + New Features

QGridLayout does not stretch the homemade QWidget over the workspace.

Scheduled Pinned Locked Moved Solved General and Desktop
2d graphicswidgetqt6
7 Posts 4 Posters 1.2k Views 1 Watching
  • 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.
  • T Offline
    T Offline
    timob256
    wrote on 19 Jul 2022, 15:04 last edited by timob256
    #1

    QGridLayout does not stretch the homemade QWidget over the workspace.

    I don't know how to fix.

    displays part of the widget, moreover, a small part, how to fix it so that it does display in the entire application.

    Снимок экрана от 2022-07-19 18-03-42.png

    here is the code:

    CMakeLists.txt

    cmake_minimum_required(VERSION 3.5)
    
    project(vikroika VERSION 0.1 LANGUAGES CXX)
    
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
    
    set(CMAKE_AUTOUIC ON)
    set(CMAKE_AUTOMOC ON)
    set(CMAKE_AUTORCC ON)
    
    set(CMAKE_CXX_STANDARD 11)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    
    find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets REQUIRED)
    find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets REQUIRED)
    
    set(PROJECT_SOURCES
            main.cpp
            rascroika.cpp
            rascroika.h
            tochkabutton.cpp
            tochkabutton.h
    )
    
    if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
        qt_add_executable(vikroika
            MANUAL_FINALIZATION
            ${PROJECT_SOURCES}
        )
    # Define target properties for Android with Qt 6 as:
    #    set_property(TARGET vikroika APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
    #                 ${CMAKE_CURRENT_SOURCE_DIR}/android)
    # For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
    else()
        if(ANDROID)
            add_library(vikroika SHARED
                ${PROJECT_SOURCES}
            )
    # Define properties for Android with Qt 5 after find_package() calls as:
    #    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
        else()
            add_executable(vikroika
                ${PROJECT_SOURCES}
            )
        endif()
    endif()
    
    target_link_libraries(vikroika PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
    
    set_target_properties(vikroika PROPERTIES
        MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
        MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
        MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    )
    
    if(QT_VERSION_MAJOR EQUAL 6)
        qt_finalize_executable(vikroika)
    endif()
    

    main.cpp

    #include "rascroika.h"
    
    #include <QApplication>
    #include <QScreen>
    #include <QDebug>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Rascroika w;
    
        QSize screenSize = qApp->screens().at(0)->availableSize();
        const int width = screenSize.width() / 2.5;
        const int height = screenSize.height() * 0.5;
        w.setGeometry((screenSize.width() - width) / 2.0,
                      (screenSize.height() - height) / 2.0,
                      width,
                      height);
    
        w.setWindowTitle("Раскройка");
    
        w.show();
        return a.exec();
    }
    

    rascroika.h

    #ifndef RASCROIKA_H
    #define RASCROIKA_H
    
    #include <QMainWindow>
    #include <QVBoxLayout>
    #include <QGridLayout>
    
    #include "tochkabutton.h"
    
    class Rascroika : public QMainWindow
    {
        Q_OBJECT
    
    public:
        Rascroika(QWidget *parent = nullptr);
        ~Rascroika();
    
    private:
    
        TochkaButton *_tochkab;
        QGridLayout  *_vbox;
    };
    #endif // RASCROIKA_H
    

    rascroika.cpp

    #include "rascroika.h"
    
    Rascroika::Rascroika(QWidget *parent)
        : QMainWindow(parent)
        , _vbox(new QGridLayout(this))
        , _tochkab(new TochkaButton(this))
    {
         _vbox->addWidget(_tochkab,0,0,Qt::AlignJustify);
    
    //    _tochkab->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        _tochkab->setPosition(this->x(),this->y());
        _tochkab->setScale(this->width(),this->height());
    
    //    _tochkab->setPosition(10,10);
    //    _tochkab->setScale(100,200);
    
        this->setLayout(_vbox);
        this->show();
    }
    
    Rascroika::~Rascroika()
    {
    }
    

    tochkabutton.h

    #ifndef TOCKABUTTON_H
    #define TOCKABUTTON_H
    
    #include <QWidget>
    #include <QPainter>
    #include <QColor>
    #include <QDebug>
    
    
    class TochkaButton : public QWidget {
        Q_OBJECT
    public:
        using QWidget::QWidget;
    
        // туту сетеры  гетеры
        ///
        /// \brief setColor выбор цвета
        ///
        void setColor(int r, int g, int b, int a);
    
        ///
        /// \brief setColor выбор цвета "основной" линии
        ///
        void setColorLine(int r, int g, int b, int a);
    
        ///
        /// \brief setScaleFactor установить коэффициент масштаба
        ///
        void setScale(float height, float width);
    
        ///
        /// \brief setPosition установить расположение обьекта
        ///
        void setPosition(float x, float y);
    
    private:
        // туту слоты
        QColor   _color        {220, 220, 0, 227};
        QColor   _color_line   {0, 0, 0, 255}; //  ченый не прозрачный
        float    _height        {0};
        float    _width         {0};
        float    _thickness     {0};        // толщина оучки
        float    _x             {0};
        float    _y             {0};
    
    
    protected:
        virtual void paintEvent(QPaintEvent *event);          // тут рисуем
    //    virtual QSize sizeHint() const;                       // тут задаем размеры (возми из иного прмера)
        virtual void mousePressEvent(QMouseEvent * event);    // тут нажатие клавиши (зажатие)
        virtual void mouseReleaseEvent(QMouseEvent * event);  // тут зажатие клавиши (отддатие  )
    };
    
    #endif // TOCKABUTTON_H
    

    tochkabutton.cpp

    #include "tochkabutton.h"
    
    void TochkaButton::setColor(int r, int g, int b, int a)
    {
        _color.setRgb(r,g,b,a);
    }
    
    void TochkaButton::setColorLine(int r, int g, int b, int a)
    {
        _color_line.setRgb(r,g,b,a);
    }
    
    void TochkaButton::setScale(float height, float width)
    {
        _height = height;
        _width = width;
    
        _thickness = (height+width)/100.0;
    }
    
    void TochkaButton::setPosition(float x, float y)
    {
        _x = x;
        _y = y;
    }
    
    void TochkaButton::paintEvent(QPaintEvent *event)
    {
        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing, true);
    
        painter.setPen(QPen(_color_line, _thickness, Qt::SolidLine));
        painter.setBrush(QBrush(_color));
    
        qDebug() << "_x" << _x << "_y" << _y << "_width" << _width << "_height" << _height;
        painter.drawRect(_x, _y, _width, _height);
    
        // проверить несколько вариантов
        painter.setBrush(QBrush(_color_line)); // чтоб центр был круглый
    //    painter.drawEllipse(_x+_width/2.0-_thickness, _y+_height/2.0-_thickness, _thickness*2, _thickness*2 );
    
        // воторой вариант просто точка
        painter.drawPoint(_x+_width/2.0, _y+_height/2.0);
    }
    
    void TochkaButton::mousePressEvent(QMouseEvent *event)
    {
    
    }
    
    void TochkaButton::mouseReleaseEvent(QMouseEvent *event)
    {
    
    }
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 19 Jul 2022, 18:42 last edited by
      #3

      Hi,

      You are using a QMainWindow as base class for Rascroika. QMainWindow already has a layout that you cannot replace. You likely also have a run time warning printed when you run your application.

      Either change the base class or create a central widget and apply your layout on that one.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      T 1 Reply Last reply 21 Jul 2022, 11:04
      2
      • C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 19 Jul 2022, 15:54 last edited by
        #2

        You don't return any size() or sizeHint() so how should Qt know how much space wour widget needs?

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 19 Jul 2022, 18:42 last edited by
          #3

          Hi,

          You are using a QMainWindow as base class for Rascroika. QMainWindow already has a layout that you cannot replace. You likely also have a run time warning printed when you run your application.

          Either change the base class or create a central widget and apply your layout on that one.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          T 1 Reply Last reply 21 Jul 2022, 11:04
          2
          • S SGaist
            19 Jul 2022, 18:42

            Hi,

            You are using a QMainWindow as base class for Rascroika. QMainWindow already has a layout that you cannot replace. You likely also have a run time warning printed when you run your application.

            Either change the base class or create a central widget and apply your layout on that one.

            T Offline
            T Offline
            timob256
            wrote on 21 Jul 2022, 11:04 last edited by
            #4

            @SGaist said in QGridLayout does not stretch the homemade QWidget over the workspace.:

            Hi,

            You are using a QMainWindow as base class for Rascroika. QMainWindow already has a layout that you cannot replace. You likely also have a run time warning printed when you run your application.

            yes

            QLayout: Attempting to add QLayout "" to Rascroika "", which already has a layout
            QWidget::setLayout: Attempting to set QLayout "" on Rascroika "", which already has a layout
            

            Either change the base class or create a central widget and apply your layout on that one.

            I don't understand what to do ;_;

            i tried to add layouts to rascroika.cpp file, gives the same error.

            rascroika.cpp

            #include "rascroika.h"
            
            Rascroika::Rascroika(QWidget *parent)
                : QMainWindow(parent)
                , _vbox(new QGridLayout(this))
                , _tochkab(new TochkaButton(this))
            {
                QVBoxLayout *vlay = new QVBoxLayout();
                QHBoxLayout *hlay1 = new QHBoxLayout();
            
                 hlay1->addWidget(_tochkab);
                 _tochkab->setPosition(this->x(),this->y());
                 _tochkab->setScale(this->width(),this->height());
            
                hlay1->addStretch(1);
                vlay->addItem(hlay1);
            
                vlay->addStretch(1);
            
                this->setLayout(vlay);
                this->show();
            }
            
            Rascroika::~Rascroika()
            {
            }
            

            I don't know how to add my _vbox widget to the layout.

            this->addWidget(_tochkab);
            

            error: no member named 'addWidget' in 'Rascroika'

            please, tell me what i need to write??????

            J 1 Reply Last reply 21 Jul 2022, 11:06
            0
            • T timob256
              21 Jul 2022, 11:04

              @SGaist said in QGridLayout does not stretch the homemade QWidget over the workspace.:

              Hi,

              You are using a QMainWindow as base class for Rascroika. QMainWindow already has a layout that you cannot replace. You likely also have a run time warning printed when you run your application.

              yes

              QLayout: Attempting to add QLayout "" to Rascroika "", which already has a layout
              QWidget::setLayout: Attempting to set QLayout "" on Rascroika "", which already has a layout
              

              Either change the base class or create a central widget and apply your layout on that one.

              I don't understand what to do ;_;

              i tried to add layouts to rascroika.cpp file, gives the same error.

              rascroika.cpp

              #include "rascroika.h"
              
              Rascroika::Rascroika(QWidget *parent)
                  : QMainWindow(parent)
                  , _vbox(new QGridLayout(this))
                  , _tochkab(new TochkaButton(this))
              {
                  QVBoxLayout *vlay = new QVBoxLayout();
                  QHBoxLayout *hlay1 = new QHBoxLayout();
              
                   hlay1->addWidget(_tochkab);
                   _tochkab->setPosition(this->x(),this->y());
                   _tochkab->setScale(this->width(),this->height());
              
                  hlay1->addStretch(1);
                  vlay->addItem(hlay1);
              
                  vlay->addStretch(1);
              
                  this->setLayout(vlay);
                  this->show();
              }
              
              Rascroika::~Rascroika()
              {
              }
              

              I don't know how to add my _vbox widget to the layout.

              this->addWidget(_tochkab);
              

              error: no member named 'addWidget' in 'Rascroika'

              please, tell me what i need to write??????

              J Offline
              J Offline
              JonB
              wrote on 21 Jul 2022, 11:06 last edited by JonB
              #5

              @timob256 said in QGridLayout does not stretch the homemade QWidget over the workspace.:

              please, tell me what i need to write??????

              @SGaist said in QGridLayout does not stretch the homemade QWidget over the workspace.:

              Either change the base class or create a central widget and apply your layout on that one.

              @timob256 said in QGridLayout does not stretch the homemade QWidget over the workspace.:

              I don't know how to add my _vbox widget to the layout.

              To which layout? someLayout->addWidget(_tochkab). Not this->addWidget(_tochkab); ("error: no member named 'addWidget' in 'Rascroika'".)

              1 Reply Last reply
              1
              • T Offline
                T Offline
                timob256
                wrote on 23 Jul 2022, 11:26 last edited by
                #6
                #include "rascroika.h"
                
                Rascroika::Rascroika(QWidget *parent)
                   : QMainWindow(parent)
                   , _vbox(new QGridLayout(this))
                   , _tochkab(new TochkaButton(this))
                {
                   _tochkab->setPosition(this->x(),this->y());
                   _tochkab->setScale(this->width(),this->height());
                
                   this->setCentralWidget(_tochkab);
                   this->show();
                }
                

                Снимок экрана от 2022-07-23 14-22-59.png

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 23 Jul 2022, 16:36 last edited by
                  #7

                  You _vbox variable is now useless, you should remove it.

                  Passing a parent to a layout when creating it automatically applies that layout to the parent. That's not what you want here (beside the fact that there are no use in setting a widget on a QMainWindow object.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1

                  2/7

                  19 Jul 2022, 15:54

                  topic:navigator.unread, 5
                  • Login

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