#include "mainwindow.h" #include "ui_mainwindow.h" #include <QGraphicsScene> #include…
- User
#include "mainwindow.h" #include "ui_mainwindow.h"
#include #include #include #include #include #include #include #include #include #include #include #include #include #include
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow), speedInput(nullptr), directionButtons(nullptr), speed(0), direction(0), timer(nullptr), object(nullptr) { ui->setupUi(this);
// Set window title setWindowTitle("Your Name - Group Number - Variant Number QT-GI-15"); // Create QGraphicsScene and set it as the central widget scene = new QGraphicsScene(this); ui->setupUi(this); ui->graphicsView->setScene(scene); // заменить на соответствующее имя QGraphicsView объекта // Create the menu createMenu();}
MainWindow::~MainWindow() { delete ui; }
void MainWindow::createMenu() { QMenu *menu = new QMenu("Menu", this);
QAction *showPictureAction = menu->addAction("Show picture"); connect(showPictureAction, &QAction::triggered, this, &MainWindow::showPicture); QAction *chooseAction = menu->addAction("Choose"); connect(chooseAction, &QAction::triggered, this, &MainWindow::choose); QAction *animateAction = menu->addAction("Animate"); connect(animateAction, &QAction::triggered, this, &MainWindow::animate); QAction *stopAction = menu->addAction("Stop"); connect(stopAction, &QAction::triggered, this, &MainWindow::stop); QAction *quitAction = menu->addAction("Quit"); connect(quitAction, &QAction::triggered, this, &MainWindow::close); menuBar()->addMenu(menu);}
void MainWindow::showPicture() { // Remove any existing object in the scene if(object != nullptr) scene->removeItem(object);
// Add a rectangle QGraphicsRectItem *rectangle = new QGraphicsRectItem(-50, -50, 100, 100); rectangle->setBrush(Qt::red); scene->addItem(rectangle);
// Add a triangle QGraphicsPolygonItem *triangle = new QGraphicsPolygonItem(); QPolygonF polygon; polygon << QPointF(-50, 0) << QPointF(50, 0) << QPointF(0, 50); triangle->setPolygon(polygon); triangle->setPos(0, -50); triangle->setBrush(Qt::green); scene->addItem(triangle);
// Add a circle QGraphicsEllipseItem *circle = new QGraphicsEllipseItem(-25, -25, 50, 50); circle->setBrush(Qt::blue); scene->addItem(circle);
// Add a trapezoid QGraphicsPolygonItem *trapezoid = new QGraphicsPolygonItem(); QPolygonF trapezoidPolygon; trapezoidPolygon << QPointF(-50, 0) << QPointF(-25, -50) << QPointF(25, -50) << QPointF(50, 0); trapezoid->setPolygon(trapezoidPolygon); trapezoid->setBrush(Qt::yellow); scene->addItem(trapezoid);
// Set the object to the center of the scene object = rectangle; object->setPos(0, 0); }
void MainWindow::choose() { // Create a dialog to get the speed and direction input QDialog *dialog = new QDialog(this); dialog->setWindowTitle("Choose options");
speedInput = new QLineEdit(dialog); speedInput->setPlaceholderText("Enter speed (pixels per second)");
- Assistant
Проблема, скорее всего, связана с ошибкой в коде, которую надо исправить. Возможно, это связано с неправильным использованием указателей или с переполнением какого-то буфера. Для диагностики проблемы необходимо проанализировать вывод консоли и логи ошибок при запуске приложения.
Однако, на первый взгляд, причина может быть связана со следующей строкой кода в функции animate():
if(direction == 0) object->setPos(0, -50); else object->setPos(-50, 0);
Если direction не принимает значение 0 или 1, то объект может переместиться за границу окна или пропасть из виду. Это может вызвать ошибку или закрытие окна. Необходимо убедиться, что значение переменной direction корректно задается и не выходит за допустимые пределы.
Также проверьте, что значения переменных speed и direction корректно передаются в функцию moveObject(), и что таймер запускается с правильными параметрами.