【发布时间】:2015-07-14 18:21:35
【问题描述】:
我今年开始学习 Qt,这是我喜欢使用的东西,我在 C++ 方面提高了很多知识。我正在做一个长期项目,我正在使用蛇算法,我正在使用 Qt 来设计界面。我已经创建了所有布局,我遇到的问题不是设计而是 QGraphicsView 和 QGraphicsScene 没有更新我不明白为什么。我想使用一个函数通过单击一个按钮在图片上添加文本,并且我的代码正在编译而没有错误,但是当我单击该按钮时崩溃了。
这里只是我用来做我想做的部分代码:
MainWindow.cpp
/*All includes done, the list is long*/
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
...
QImage image1("2.jpg");
QGraphicsPixmapItem *item = new QGraphicsPixmapItem;
item->setPixmap(QPixmap::fromImage(image1));
QGraphicsScene *scene = new QGraphicsScene;
scene->addItem(item);
//scene->addLine(QLineF(256, 256, 20, 20));
/*HERE, if I uncomment this line above, the line will be added correctly without problems*/
QGraphicsView *view = new QGraphicsView;
view->setScene(scene);
...
QPushButton *imageProcessing1 = new QPushButton("Image Processing 1");
QObject::connect( imageProcessing1, SIGNAL(clicked()), this, SLOT(testt()));
...
}
void MainWindow::testt()
{
scene->addLine(QLineF(256, 256, 20, 20));
}
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QImage>
#include <QGraphicsPixmapItem>
class ProjectWindow;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
QGraphicsScene *scene;
public slots:
void runSnakes();
void drawCircle(int x, int y);
void testt();
};
#endif // MAINWINDOW_H
main.cpp
#include "winimage.h"
#include "snakes.h"
#include <iostream> // standard C++ I/O
#include <fstream>
#include <windows.h>
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
auto win = new MainWindow;
win->show();
return app.exec();
}
所以,我想了解它为什么会崩溃?我对函数“testt”做错了吗?我的意思是,这可能不是我应该做的。我在使用QFileDialog加载图像时也遇到了这个问题,并且图像根本没有显示,并且仍然没有解决,我看了很多方法,它根本不起作用。
提前感谢您阅读我的问题并帮助我。如果需要,我可以通过私人邮件发送整个项目(仅源文件)。
【问题讨论】:
-
Qt Creator 是一个 IDE,它与问题无关。