【发布时间】:2015-10-01 23:59:06
【问题描述】:
我正在尝试了解 Qt 的基础知识。浏览了一些帖子后,我知道ui_mainwindow.h 是由UIC tool 创建的,ui_mainwindow.h 包含有关我创建的表单/ui 的信息。
在我的GUI 中,我使用了一个按钮和一个图形视图。我想要一个简单的图像(我在程序本身内部创建)显示在graphicsView 中。我正在尝试通过两种方式(出于学习目的):
- 我可以在
on_pushButton_clicked()(即我的push_button的插槽)中编写代码。 - 我正在尝试将图像从
main()
问题:我已经完成了第一种方法。我在on_pushButton_clicked() 中使用了以下代码行,并且成功了。
void MainWindow::on_pushButton_clicked()
{
//Display image in the graphics viewer
Mat img(200,200, CV_8UC3, Scalar(255,0,0));
QImage image( img.data, img.cols, img.rows, img.step, QImage::Format_RGB888 );
QGraphicsScene* scene = new QGraphicsScene();
QGraphicsPixmapItem* item = new QGraphicsPixmapItem(QPixmap::fromImage(image));
scene->addItem(item);
ui->graphicsView->setScene(scene);
}
现在,我想从main() 做类似的事情。为此,现在我的main() 如下所示:
#include "mainwindow.h"
#include <QApplication>
//For image
#include <QImage>
#include <QPixmap>
#include <QGraphicsPixmapItem>
//#include "ui_mainwindow.h"
//OPENCV Headers
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs/imgcodecs.hpp>
using namespace cv;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
//Display image in the graphics viewer
Mat img(200,200, CV_8UC3, Scalar(255,0,0));
QImage image( img.data, img.cols, img.rows, img.step, QImage::Format_RGB888 );
QGraphicsScene* scene = new QGraphicsScene();
QGraphicsPixmapItem* item = new QGraphicsPixmapItem(QPixmap::fromImage(image));
scene->addItem(item);
w.ui->graphicsView->setScene(scene);
w.show();
return a.exec();
}
如果我将#include "ui_mainwindow.h" 放在main.cpp 中,上面写在main() 中的代码就可以工作。但是,如果我评论 #include "ui_mainwindow.h" 和 w.ui->graphicsView->setScene(scene);,那么它会为 QGraphicsScene* scene = new QGraphicsScene(); 引发错误。
错误是main.cpp:32: error: allocation of incomplete type 'QGraphicsScene' QGraphicsScene* scene = new QGraphicsScene();
问题:为什么QGraphicsScene 和"ui_mainwindow.h" 之间存在联系。我知道我需要"ui_mainwindow.h" 用于w.ui->graphicsView->setScene(scene); 行,因为我在那里使用我的ui,但我不明白需要QGraphicsScene。
【问题讨论】:
-
你需要包含
QGraphicsScene:#include <QGraphicsScene>。它包含在ui_mainwindow.h中,这就是为什么包含该标头时不会出现该错误的原因。 -
@thuga:谢谢,这是一个愚蠢的错误。
标签: qt