QGridLayout does not stretch the homemade QWidget over the workspace.
-
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.
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) { }
-
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.
-
You don't return any size() or sizeHint() so how should Qt know how much space wour widget needs?
-
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.
-
@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??????
-
@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)
. Notthis->addWidget(_tochkab);
("error: no member named 'addWidget' in 'Rascroika'".) -
#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(); }
-
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.