Changing text in QGraphicsSimpleTextItem SOLVED
-
I have a windows desktop application that I am developing. I had 2 widgets that display changing information from other parts of the application. I've had it working fine for a number of weeks. Due to a requested design change from the user I had to split the two widgets into 4 and reposition them on the ui. After completing the design changes I have run into a very crazy problem.
All the widgets on the ui are based on QGraphicsView. The widgets that display information have QGraphicsSimpleTextItem to display headers and application information. The widgets have slots that accept information and perform miodify the information fields.
Now when the text items are changed the old value is not replaced by the new value. It is overwritten. I have compared the old and new code line by line. Other than the number of fields on the display the code is exactly the same. I can't determine what is the cause of the behavior change.
I have built a sample of the code that repeats this behavior. I hope someone can identify what is causing this to happen. The sample has a widget that simply accept input from a timer that is a counter. This is the best way I could determine how to duplicate the logic flow of the actual application.
I tried to attach the code necessary to run this. But it was too large. Here is the code for the widget that does the updates.
test1Widget.h
@#ifndef TEST1WIDGET_H
#define TEST1WIDGET_H#include <QGLWidget>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsSimpleTextItem>
#include <QFont>
#include <QRect>#include <QGraphicsView>
class Test1Widget : public QGraphicsView
{
Q_OBJECT
public:
explicit Test1Widget(QWidget *parent = 0);
~Test1Widget();private:
QGraphicsSimpleTextItem* displayValue;
QGraphicsSimpleTextItem* displayHdr;QGraphicsSimpleTextItem* genHeader(QString&, int, int);
QGraphicsSimpleTextItem* genText(QString&, int, int);
QGraphicsScene scene;
QGLWidget glWidget_;QFont hdrFont;
QFont txtFont;
QBrush hdrBrush;
QBrush txtBrush;public slots:
void updateInfo(int);};
#endif // TEST1WIDGET_H
@test1widget.cpp
@#include "test1widget.h"Test1Widget::Test1Widget(QWidget *parent) :
QGraphicsView(parent),
hdrBrush(Qt::gray, Qt::SolidPattern),
txtBrush(Qt::yellow, Qt::SolidPattern),
hdrFont("Arial"),
txtFont("Arial")
{
this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
this->setStyleSheet("QGraphicsView{border:2px groove #aaaaaa;;background-color:transparent;}");
this->glWidget_ = new QGLWidget(QGLFormat(QGL::SampleBuffers));
this->setViewport(glWidget_);
setAutoFillBackground(false);
this->glWidget_->setAutoFillBackground(false);
this->scene = new QGraphicsScene(this);
this->setScene(this->scene);
this->scene->setSceneRect(this->geometry());this->hdrFont.setPointSize(10);
this->hdrFont.setWeight(50);
this->hdrFont.setBold(true);this->txtFont.setPointSize(24);
this->txtFont.setWeight(95);
this->txtFont.setBold(true);this->displayHdr = genHeader(QString("Test 1"), 10,20);
this->displayValue = genText(QString("*"), 10,30);
this->scene->update();
}Test1Widget::~Test1Widget()
{
delete this->scene;
delete this->glWidget_;
}QGraphicsSimpleTextItem* Test1Widget::genHeader(QString& text, int x, int y)
{
QGraphicsSimpleTextItem* rtn = scene->addSimpleText(text, hdrFont);
rtn->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
rtn->setBrush(this->hdrBrush);
rtn->setPos(x,y);return rtn;
}QGraphicsSimpleTextItem* Test1Widget::genText(QString& text, int x, int y)
{
QGraphicsSimpleTextItem* rtn = scene->addSimpleText(text, txtFont);
rtn->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
rtn->setBrush(this->txtBrush);
rtn->setPos(x,y);return rtn;
}void Test1Widget::updateInfo(int data)
{
this->displayValue->setText(QString::number(data));
this->scene->update();
}
@ -
Qt 5.3.1 Qt Creator 3.1.2
-
I have resolved the issue. These sorts of things I am sure are easy for experienced Qt developers. However it is not so obvious to us newbies.
I had to add one line of code. This was to add a background brush to the scene.
-
-
I didn't have it in there because I prefer to have that widget transparent to the underlying widget. I just didn't realize that the background brush of the scene was critical to the process of changing the text values. I think the issue of transparent view/scene is an issue for another day.