Using graphicsview in a class and link to MainWindow form
-
Hi all, I'm working on a class that inherits a graphicsview and creates a new graphicsview instance. The problem is that the graphicsview comes out in a seperate window (apart from MainWindow), but I'd like to link it with the MainWindow form.
Normally I would pass ui->centralwidget to the graphicsview from the MainWindow,
[code]
QGraphicsView *graphicsArea= new QGraphicsView(ui->centralwidget)
[/code]in order to link the graphicsview with the MainWindow form. But since I'm doing it in a class, I've tried passing ui->centralwidget to the class but did not have any success. Can you please recommend any tips on a how to make this work? Thanks.
[code]
// Standard MainWindow.h generated by the qt compiler#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);}
// Standard MainWindow.cpp generated by the qt compiler
#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);//Declare new graphics window
NewGraphicsWindow graphicsArea;graphicsArea.window->setGeometry(QRect(50, 50, 400, 200));
graphicsArea.window->show();}
// Class NewGraphicsWindow
#ifndef NEWGRAPHICSWINDOW_H
#define NEWGRAPHICSWINDOW_H#include <QGraphicsView>
#include <QGraphicsScene>class NewGraphicsWindow:QGraphicsView,QGraphicsScene
{
public:
NewGraphicsWindow();QGraphicsView *window = new QGraphicsView();
};
#endif // NEWGRAPHICSWINDOW_H
[/code]
-
welcome to the forum.
@ NewGraphicsWindow graphicsArea;
graphicsArea.window->setGeometry(QRect(50, 50, 400, 200));
graphicsArea.window->show();@You are calling show separately. That is why it launches it separatey. also you are creating the stack variable there.
@Did you try MainWindow->setCentralWidget(graphicsView).@
-
I see, thanks for the reply. I've tried it but it's giving an error "expected unqualified-id before '->' token."
-
Can you paste your code here on how you are doing. Here problem may be due to stack variable used with -> rather than ".". Just check. What you are hitting is compilation issue and you may solve the problem easily.
-
Sure. Here is the MainWindow code. I'm not sure if I'm using your suggestion correctly.
[code]
#include "mainwindow.h"
#include "ui_mainwindow.h"#include "newgraphicswindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);//Declare new graphics window NewGraphicsWindow graphicsArea; MainWindow->setCentralWidget(graphicsArea); graphicsArea.window->setGeometry(QRect(50, 50, 400, 200)); graphicsArea.window->show();
}
[/code] -
Hi,
In addition, this:
@
class NewGraphicsWindow:QGraphicsView,QGraphicsScene{
public:
NewGraphicsWindow();QGraphicsView *window = new QGraphicsView();
};
@looks wrong in many ways. You can't subclass from both since they both inherit from QObject, and there's also no sense for. A QGraphicsScene is meant to be set on a QGraphicsView.
Also, why do you have a public member QGraphicsView * ?
-
[quote author="SGaist" date="1409687502"]Hi,
In addition, this:
@
class NewGraphicsWindow:QGraphicsView,QGraphicsScene{
public:
NewGraphicsWindow();QGraphicsView *window = new QGraphicsView();
};
@looks wrong in many ways. You can't subclass from both since they both inherit from QObject, and there's also no sense for. A QGraphicsScene is meant to be set on a QGraphicsView.
Also, why do you have a public member QGraphicsView * ?[/quote]
First I'd just like to create a QGraphicsView in a class and have it put in the MainWindow UI form, instead of it being launched separately. If it works, I can add the QGraphicsScene later.
No reason why QGraphcicsView is public, the sample code is just a test program which creates a QGraphicsView separately, and MainWindow form separately. Making it private will not change it.
-
It wasn't a public vs private question, but why are you inheriting from a class and keep a member variable of that same class in that class ?
-
@MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QGraphicsView *view = new QGraphicsView;
QGraphicsScene *scene = new QGraphicsScene;
view->setScene(scene);
for (int x=0;x<100;x+=10){
for (int y=0;y<100;y+=10){
QGraphicsEllipseItem *item= new QGraphicsEllipseItem(x,y,8,8);
item->setBrush(Qt::magenta);
scene->addItem(item);
}
}
this->setCentralWidget(view);
}
@