Hi!
Thanks for your replies.
But my Problem is still not solved.
I have changed my strategy a bit:
I am extensively working on a QGraphicsView and Scene, so the idea is to hide all QGraphicsItems in my scene once the computation starts, and to show a Progressbar which is a subclass of QGraphicsItem.
the code looks like that:
//MainView is derived from QGraphicsView
void MainView::loadBackgroundImage(const QString& filename)
{
// the background item shall always be newly created.
if(this->scene()->items().contains(this->m_background))
this->scene()->removeItem(this->m_background);
delete m_background;
//hide all scene items
this->m_scalable_item->setVisible(false);
//get the progressbar (derived from QGraphicsItem)
GraphicsProgressBar * g = new GraphicsProgressBar;
this->scene()->addItem(g);
g->setPos(this->sceneRect().center());
this->scene()->update();
connect(timer, SIGNAL(timeout()), g, SLOT(updateProgress()));
timer->start(100);
//BackgroundImageItem is derived from QObject, QThread and QGraphicsItem
// start() starts the computation is background-thread
m_background = new BackgroundImageItem(filename);
m_background->start();
qDebug() << "work started";
//the gui thread should wait here until the worker has finished
m_background->wait();
qDebug
() <<
QThread::currentThreadId() <<
"Waiting done.";
// I just add the background item here
this->scene()->addItem(this->m_background);
if(!this->scene()->items().contains(m_scalable_item))
this->scene()->addItem(m_scalable_item);
this->m_scalable_item->setVisible(true);
//free all timer resoureces and the progressbar
timer->stop();
timer->disconnect();
g->disconnect();
this->scene()->removeItem(g);
delete g;
delete timer;
}
//MainView is derived from QGraphicsView
void MainView::loadBackgroundImage(const QString& filename)
{
// the background item shall always be newly created.
if(this->scene()->items().contains(this->m_background))
this->scene()->removeItem(this->m_background);
delete m_background;
//hide all scene items
this->m_scalable_item->setVisible(false);
//get the progressbar (derived from QGraphicsItem)
GraphicsProgressBar * g = new GraphicsProgressBar;
this->scene()->addItem(g);
g->setPos(this->sceneRect().center());
this->scene()->update();
QTimer * timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), g, SLOT(updateProgress()));
timer->start(100);
//BackgroundImageItem is derived from QObject, QThread and QGraphicsItem
// start() starts the computation is background-thread
m_background = new BackgroundImageItem(filename);
m_background->start();
qDebug() << "work started";
//the gui thread should wait here until the worker has finished
m_background->wait();
qDebug() << QThread::currentThreadId() << "Waiting done.";
// I just add the background item here
this->scene()->addItem(this->m_background);
if(!this->scene()->items().contains(m_scalable_item))
this->scene()->addItem(m_scalable_item);
this->m_scalable_item->setVisible(true);
//free all timer resoureces and the progressbar
timer->stop();
timer->disconnect();
g->disconnect();
this->scene()->removeItem(g);
delete g;
delete timer;
}
To copy to clipboard, switch view to plain text mode
The Progressbar code looks like:
{
Q_OBJECT
public:
GraphicsProgressBar()
{
setZValue(5);
status = 1;
};
inline QRectF boundingRect
() const { return QRectF(-66,
-21,
132,
42);
};
{
qDebug() << "paint";
painter->drawRect(-66,-21,132,42);
if(status >=1)
painter->drawRect(-65,-20,40,40);
if(status >=2)
painter->drawRect(-20,-20,40,40);
if(status >=3)
painter->drawRect(25,-20,40,40);
};
public slots:
void updateProgress()
{
status = (status+1)%4;
qDebug
() <<
QThread::currentThreadId() << status;
//this->update();
};
private:
int status;
};
class GraphicsProgressBar : public QObject, public QGraphicsItem
{
Q_OBJECT
public:
GraphicsProgressBar()
{
setZValue(5);
status = 1;
};
inline QRectF boundingRect() const { return QRectF(-66,-21,132,42); };
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
qDebug() << "paint";
painter->drawRect(-66,-21,132,42);
if(status >=1)
painter->drawRect(-65,-20,40,40);
if(status >=2)
painter->drawRect(-20,-20,40,40);
if(status >=3)
painter->drawRect(25,-20,40,40);
};
public slots:
void updateProgress()
{
status = (status+1)%4;
qDebug() << QThread::currentThreadId() << status;
//this->update();
};
private:
int status;
};
To copy to clipboard, switch view to plain text mode
The progressbar does not show up.
Also, the hiding
The paint method gets never called!
Also the debugmsg from updateProgress never appears.
It seems that nothing I try to change at the graphicsScene or view has an effect.
Its not even possible to hide all elements.
HELP appreciated!
Bookmarks