PDA

View Full Version : Add Static area to QGraphicsView



dubstar_04
12th April 2011, 19:16
I am in the process of making a tvguide. I have used a QGraphicsView and added QGraphicsItems with the program name. I would like to add static areas, one on the left to display the channel name, icon and one at the top to display a timebar.

could you suggest a method of achieving this?

the ui has been draw using QTCreator, not hand coded. I have looked at setViewPortMargins but i dont how i would use this with the standard QGraphicsView from Creator.

Any help or guidance much appreciated.

https://lh6.googleusercontent.com/_yGkw0Cr-R9E/TaSVRnrfRSI/AAAAAAAAB_c/1Zc7yhcAuCM/s720/Screen%20shot%202011-04-12%20at%2019.08.26.png

wysota
12th April 2011, 21:01
There are many ways to do that. One would indeed be to use viewport margins, another would be to put one view over another one. You could also simply use regular items and move them to appropriate positions when the view is scrolled.

dubstar_04
12th April 2011, 22:11
Thanks for your reply. in your opinion what would be the 'best' approach?

I looked at drawing regular shapes over the view and moving them as the view moves, therefore, them seeming to stay in the same position. I couldn't workout how to collect a signal from the view to indicate that it had moved.

could you advise?

Thanks,

Dubstar_04

wysota
12th April 2011, 23:15
Thanks for your reply. in your opinion what would be the 'best' approach?
It's not possible to say what's best until you try something. I don't know your specific use-case.

I couldn't workout how to collect a signal from the view to indicate that it had moved.
The view has scroll bars that emit a valueChanged() signal.

dubstar_04
13th April 2011, 11:07
i have been looking at this and i dont know how to collect the valueChanged() signal.

i tried:


connect(ui->graphicsView->verticalScrollBar(), SIGNAL(valueChanged(int)),this, SLOT(warning(int value)));

and there is no signal available there.

I have also tried this(with no result):


QAbstractSlider *verticalSlider = new QAbstractSlider(ui->graphicsView->verticalScrollBar());

connect(verticalSlider, SIGNAL(sliderMoved(int)), SLOT(warning(int value)) );

dubstar_04
13th April 2011, 15:24
can anyone direct me how to connect the valueChanged() signal of the the scrollBar to a slot?

I have tried everything i can think of.

The last thing i have tried is subclassing QGraphicsView and calling the connect from there but the signal is unavailable.

wysota
13th April 2011, 15:44
How did you implement warning()? Does connect() return true?

dubstar_04
13th April 2011, 16:35
warning() is just a slot that prints the int value via std::cout.


connect(ui->graphicsView->verticalScrollBar(), SIGNAL(valueChanged(int)),this, SLOT(warning(int value)));

wont compile:

https://lh4.googleusercontent.com/_yGkw0Cr-R9E/TaXG1uRsUtI/AAAAAAAAB_4/tXfaOuq-RPg/Screen%20shot%202011-04-13%20at%2016.51.11.png

I dont know how to connect the ui->graphicsView->verticalScrollBar() to the slot or even if thats the correct object to connect.

Thanks,

Dubstar_04

stampede
13th April 2011, 16:50
connect(ui->graphicsView->verticalScrollBar(), SIGNAL(valueChanged(int)),this, SLOT(warning(int /*no parameter name here*/ )));
You can't give parameter name in SIGNAL or SLOT macro, only types.

dubstar_04
13th April 2011, 16:56
I added a screenshot of the build error. error is still the same with:


connect(ui->graphicsView->verticalScrollBar(), SIGNAL(valueChanged(int)),this, SLOT(warning(int)));

stampede
13th April 2011, 17:03
I added a screenshot of the build error. error is still the same
Ok, I haven't seen the error before posting.
Maybe it's a stupid question, but - is the MainWindow a subclass of QObject (QWidget,QMainWindow,...) ? Does this class have Q_OBJECT macro in private section ?
QScrollBar is QObject-based for sure. Can you show the class header ?

dubstar_04
13th April 2011, 17:09
This is the MainWindow class header.

Normally when i am typing the connections, QTCreator offers a list of signals that can be emitted by the object. when i am typing this connection in nothing is offered.


class MainWindow : public QMainWindow
{
Q_OBJECT

QNetworkAccessManager manager2;
QList<QNetworkReply *> currentDownloads;

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

Thanks,

Dubstar_04

stampede
13th April 2011, 17:54
This is the MainWindow class header.
Well, I can agree that this could be a part of MainWindow class header :)

There should be nothing wrong in your code, my quick test looks very similar and works:


// test.h
#include <QtGui>

class MainW : public QMainWindow{
Q_OBJECT

QGraphicsView * view;

public:
explicit MainW( QWidget * parent = NULL );
public slots:
void warning( int value ){
this->setWindowTitle(QString::number(value));
}
};

// test.cpp
#include "test.h"

MainW::MainW( QWidget * parent ) : QMainWindow(parent){
view = new QGraphicsView(this);
QGraphicsScene * scene = new QGraphicsScene(this);
scene->setSceneRect(0,0,1000,1000);
view->setScene(scene);
this->setCentralWidget(view);
connect( view->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(warning(int)) );
}

int main(int argc, char *argv[]){
QApplication a(argc, argv);
MainW m;
m.show();
return a.exec();
}


Try to make a clean build ( make clean, qmake, make ). If this doesn't help, post full code (if you can).

dubstar_04
13th April 2011, 18:23
stampede,

I just made a new project and copied your example and i get the same error.

if i edit out the connect line it builds and runs as expected?

am using QT 4.7 on a mac.

any further suggestions?

Thanks,

Dubstar_04

stampede
13th April 2011, 18:35
That's really weird, I'm sorry but I can't help you more, because my experience with mac == 0 :)
I see that you have Unix and Windows in your profile info, does all of this works on those systems, can you check that ?

dubstar_04
13th April 2011, 18:39
i am just uninstalling all the Qt stuff off my mac as i have loads of different versions. I will try and build it again once i have reinstalled everything.

I will try the linux and windows machines if that doesnt work.

Thanks for all your help.

update:
Reinstalled qt on mac. same error.
it wouldnt build on linux either. same issue.

wysota
13th April 2011, 22:43
I added a screenshot of the build error. error is still the same with:


connect(ui->graphicsView->verticalScrollBar(), SIGNAL(valueChanged(int)),this, SLOT(warning(int)));

This usually means you forgot to #include <QScrollBar> or "mainwindow.h" and your compiler doesn't know QScrollBar or MainWindow are subclasses of QObject.

dubstar_04
14th April 2011, 09:33
Wysota,

Thanks for your reply. I thought i had included <QScrollBar>.

I will have a look later as i am back in work today :(

I wish i was a professional developer. its so interesting.

dubstar_04
14th April 2011, 17:58
SUCCESS !!!!

it was just missing a <QScrollBar> include.

I cant thank you guys enough for the help.

I have now drawn the timebar over the guide and it works beautifully!!

Thanks,

Dubstar_04

stampede
15th April 2011, 07:15
I don't understand something, dubstar_04 said that he couldn't compile my example with Qt 4.7, and that #include <QScrollBar> helped. But doesn't #include <QtGui> including all QtGui module classes definitions ? That's what documentation states:


To include the definitions of both modules' classes, use the following directive:
#include <QtGui>

It works without including separate header for QScrollBar on my side (Qt 4.5.2 on windows xp and Qt 4.7.2 on Ubuntu 9.02).