Early09
14th January 2011, 09:54
Hello all,
I am new in Qt programming.
I have a few problems with the QGraphicsView widget.
If I use the following code, a QGraphicsView window will open and show me "Hello".
#include <QtGui/QApplication>
#include <QtGui>
int main(int argc, char *argv[])
{
//scene.addPixmap(QPixmap('pic.jpg'));
QApplication app(argc, argv);
QGraphicsScene scene;
QGraphicsView grview;
scene.addText("Hello!");
grview.setScene(&scene);
grview.show();
return app.exec();
}
But if I will use the Designer and have following:
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
public slots:
void slot1();
};
#endif // MAINWINDOW_H
//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::slot1()
{
QGraphicsScene scene;
scene.addText("Hello!");
ui->graphicsView->setScene(&scene);
ui->graphicsView->show();
}
//main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>636</width>
<height>503</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QGraphicsView" name="graphicsView">
<property name="geometry">
<rect>
<x>20</x>
<y>10</y>
<width>256</width>
<height>192</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>90</x>
<y>280</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>636</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections>
<connection>
<sender>pushButton</sender>
<signal>clicked()</signal>
<receiver>MainWindow</receiver>
<slot>slot1()</slot>
<hints>
<hint type="sourcelabel">
<x>120</x>
<y>328</y>
</hint>
<hint type="destinationlabel">
<x>240</x>
<y>297</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>slot1()</slot>
</slots>
</ui>
Nothing will be appear on the QGraphicsView, which I placed with the QtCreator gui designer.
I don't know what I can do, to solve the problem. Thank you in advance for your help.
Best regards
Ps.: Windows 7 x64, Qt Creator 2.0.1 with Qt 4.7.0
I am new in Qt programming.
I have a few problems with the QGraphicsView widget.
If I use the following code, a QGraphicsView window will open and show me "Hello".
#include <QtGui/QApplication>
#include <QtGui>
int main(int argc, char *argv[])
{
//scene.addPixmap(QPixmap('pic.jpg'));
QApplication app(argc, argv);
QGraphicsScene scene;
QGraphicsView grview;
scene.addText("Hello!");
grview.setScene(&scene);
grview.show();
return app.exec();
}
But if I will use the Designer and have following:
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
public slots:
void slot1();
};
#endif // MAINWINDOW_H
//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::slot1()
{
QGraphicsScene scene;
scene.addText("Hello!");
ui->graphicsView->setScene(&scene);
ui->graphicsView->show();
}
//main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>636</width>
<height>503</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QGraphicsView" name="graphicsView">
<property name="geometry">
<rect>
<x>20</x>
<y>10</y>
<width>256</width>
<height>192</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>90</x>
<y>280</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>636</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections>
<connection>
<sender>pushButton</sender>
<signal>clicked()</signal>
<receiver>MainWindow</receiver>
<slot>slot1()</slot>
<hints>
<hint type="sourcelabel">
<x>120</x>
<y>328</y>
</hint>
<hint type="destinationlabel">
<x>240</x>
<y>297</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>slot1()</slot>
</slots>
</ui>
Nothing will be appear on the QGraphicsView, which I placed with the QtCreator gui designer.
I don't know what I can do, to solve the problem. Thank you in advance for your help.
Best regards
Ps.: Windows 7 x64, Qt Creator 2.0.1 with Qt 4.7.0