Код: Выделить всё
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QGraphicsScene* scene = new QGraphicsScene(this);
ui->view->setScene(scene);
QGraphicsWidget *grid = new QGraphicsWidget;
scene->addItem(form);
QRect rcontent = ui->view->contentsRect();
ui->view->setSceneRect(0, 0, rcontent.width(), rcontent.height());
QGraphicsGridLayout* grid = new QGraphicsGridLayout(grid);
ui->view->setAlignment(Qt::AlignTop|Qt::AlignLeft);
qreal top = 0;
qreal left = 0;
qreal gridsize = 15;
for (int i=0; isetFlag(QGraphicsItem::ItemIsMovable);
layout->addItem(item, i,j);
}
}
}
Код: Выделить всё
#ifndef MYSQUARE_H
#define MYSQUARE_H
#include
#include
#include
#include
class MySquare : public QWidget
{
private:
QRectF myrect;
public:
MySquare();
// QRectF boundingRect() const;
void paintEvent(QPaintEvent *event);
bool pressed;
QSize sizeHint() const override;
};
#endif // MYSQUARE_H
Код: Выделить всё
#include "MySquare.h"
MySquare::MySquare() {
pressed=false;
}
void MySquare::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
// QRectF rec = boundingRect();
QBrush brush (Qt::blue);
if (pressed){
brush.setColor(Qt::red);
}
else{
brush.setColor(Qt::green);
}
painter.fillRect(rect(), brush);
painter.drawRect(rect());
}
QSize MySquare::sizeHint() const
{
return {50,50};
}
Код: Выделить всё
MainWindow
0
0
800
600
MainWindow
0
0
831
641
0
0
800
33

В конце я хочу добиться того, чтобы эта сетка занимала 100% высоты главного окна и 66% его ширины. Квадраты должны заполнить ширину сетки и изменить размер. Кажется, я не могу понять, как создать представление динамического размера, что должно быть хорошим началом. Буду очень признателен за помощь.
Подробнее здесь: https://stackoverflow.com/questions/798 ... ain-window
Мобильная версия