如附件中的工程,就希望能够添加一个形状,但直接是在Designer中设计的QGraphicsView中显示出来,怎么总是无法显示呢??
我感觉view和scene的关系好像没有搞懂,但有觉得差不多了,木乱中。
谢谢。
mainwindowdialog.cpp
- C/C++ code
#include "mainwindowdialog.h"#include "ui_mainwindowformdialog.h"#include <QtGui>MainWindowDialog::MainWindowDialog(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindowFormDialog){ ui->setupUi(this); viewer = ui->mwGraphicsView;}MainWindowDialog::~MainWindowDialog(){ delete ui;}void MainWindowDialog::on_action_New_triggered(){ //Settings, Nothing??? viewer->setForegroundBrush(QColor(0, 200, 00, 50)); viewer->setBackgroundBrush(QPixmap("./GruBear03.png")); //[color=#FF0000]Error here..[/color]. //QGraphicsRectItem *recItem = new QGraphicsRectItem(0, 0, 100, 60); //viewer->scene()->addItem(recItem); //Error}
mainwindowdialog.h
- C/C++ code
#ifndef MAINWINDOWDIALOG_H#define MAINWINDOWDIALOG_H#include <QMainWindow>namespace Ui {class MainWindowFormDialog;}class MainWindowDialog : public QMainWindow{ Q_OBJECT public: explicit MainWindowDialog(QWidget *parent = 0); ~MainWindowDialog(); private slots: void on_action_New_triggered();private: Ui::MainWindowFormDialog *ui; //view class QGraphicsView *viewer;};#endif // MAINWINDOWDIALOG_H
main.cpp
- C/C++ code
#include <QtGui/QApplication>#include "mainwindowdialog.h"int main(int argc, char *argv[]){ QApplication a(argc, argv); MainWindowDialog w; w.show(); return a.exec();}
pro 文件
- C/C++ code
#-------------------------------------------------## Project created by QtCreator 2012-09-28T15:40:57##-------------------------------------------------QT += core guiTARGET = GraViewTEMPLATE = appSOURCES += main.cpp\ mainwindowdialog.cppHEADERS += mainwindowdialog.hFORMS += mainwindowformdialog.ui
------解决方案--------------------
一般是构造函数中这样
m_scene = new QGraphicsScene();
ui->graphicsView->setScene(m_scene);
这样的哈。。。。