【问题标题】:Understanding QGraphicsScene in Qt了解 Qt 中的 QGraphicsScene
【发布时间】: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-&gt;graphicsView-&gt;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-&gt;graphicsView-&gt;setScene(scene); 行,因为我在那里使用我的ui,但我不明白需要QGraphicsScene

【问题讨论】:

  • 你需要包含QGraphicsScene:#include &lt;QGraphicsScene&gt;。它包含在ui_mainwindow.h 中,这就是为什么包含该标头时不会出现该错误的原因。
  • @thuga:谢谢,这是一个愚蠢的错误。

标签: qt


【解决方案1】:

如果您只想绘制静态图像,最好使用QLabel 并在某些图像中设置

QImage *image = new QImage (":/prefix/image/vl.jpg" );
QLabel *magelab = new QLabel();
magelab->setPixmap(QPixmap::fromImage(*image));

一般QGraphicsScene 更好地使用连接QGraphicsView. 这意味着QGraphicsViewQGraphicsScene 设置一些对象(场景)就像:

class SomeObject : public QGraphicsView 
{
    Q_OBJECT
public:
    explicit Schemat(QWidget *parent = 0);

private:
    QGraphicsScene *scene;


};

来源

Schemat::Schemat( QWidget *parent) : QGraphicsView(parent)
{

    scene = new QGraphicsScene(this);
    this->setScene(scene);
   // to Your scene You can add some image for example
    scene->addPixmap(SOME_PIXMAP)

}

然后您创建主窗口并添加您的QGraphicsView,例如作为QGroupBox 的某些部分

void MainWindow::SetupSchemat()
{
    schema = new Schemat();
    QGroupBox *schbox;
    QHBoxLayout *hschbox;
    schbox = new QGroupBox(this); 
    hschbox = new QHBoxLayout(this);
    schbox->setTitle("SomeScene");
    hschbox->addWidget(schema); //add scene to layout
    schbox->setLayout(hschbox);
}

QGraphicsScene 就像 Your MainWindow 的某些部分,您可以在上面制作一些动画,您可以绘制一些东西。 QGraphicsScene 更适合使用,如果您不仅想使用动画,它还提供更多选项来操作图像(例如缩放、捕捉鼠标点击、通过坐标管理其他对象),并且其他对象每个都应该是动画或只是显示。到QGraphicsScene 您可以依次添加一些QGraphicsItem,每个QGraphicsItem 可以移动到QGraphicsScene,并带有错误或流动定义的特定条件。 QGraphicsViewQGraphicsSceneQGraphicsItem 一起可以在您的主窗口的某些部分创建动画或仅图像。

也很好解释你会在这里找到

https://www.youtube.com/watch?v=fmSs2mNGh9I

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-21
    • 1970-01-01
    • 2015-12-27
    • 2013-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多