Josh Kurland Josh Kurland -4 years ago 137
C++ Question

How to change the position of QTextEdit

I have a QTextEdit object that I use to display text, line by line, for OSD purposes. I would like to be able to change the position of this text box around the window without using a mouse.

The issue is that when I "move" the QTextEdit text box, all of the previous text boxs continue to exist alongside the new one.

I have been using setGeometry(x,y,w,h) to configure the position and size of the text box when I create it. I have also experimented with resize(w,h) and move(x,y), which seem to work similarly to setGeometry(). If all I want to do is to show some text, is QTextEdit the right part or is there something better? I am using Qt 4.8.

header.h

class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

void draw_osd();
QTextEdit *osd; // Make osd a member

public slots:
void val_changed();
};


code.cc

// These globals are set in a separate thread
// that takes user input to resize the QTextEdit box geometry.
int g_xpos;
int g_ypos;
int g_w;
int g_h;

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
// Create a new QTextEdit text box
osd = new QTextEdit(this);

// Set a Qtimer to update the OSD display every 1 second
QTimer *timer = new QTimer();
connect(timer, SIGNAL(timeout()), SLOT(val_changed()));
timer->start(1000);
}

MainWindow::val_changed()
{
osd->setReadOnly(true);

if (g_val_update)
{
g_val_update = false;
// Update the OSD
draw_osd();
}
}

void MainWindow::draw_osd()
{
// Clear the old data from the OSD
osd->clear();

// Set the geometry based on user input
osd->setGeometry(g_xpos, g_ypos, g_w, g_h);

QString arg = QString("OSD position %1 %2).arg(g_xpos).arg(g_ypos);
osd->append(arg);
QString arg = QString("OSD size %1 %2).arg(g_w).arg(g_h);
osd->append(arg);

osd->show();
}


I can append osd with new text from the user. So long as I do not change the geometry, the text will update in the text box nicely. I suspect the issue has to do with osd going out of scope. But if that is on the right track, why can I append the text? Am I on the right track?

Update 6/09/2017:
I made some changes to the code so that QTextEdit *osd is a member of the MainWindow class. This should limit the number of QTextEdit objects I create to one, yet I still get old data when I change the position.

Update 6/12/2017:
I found a bug after making QTextEdit *osd a member of the class where the text would be added to the same text box but never cleared. I did not see this earlier because I was restricting the size of the box to perfectly fit my text. To remedy this, I added osd->clear(); to the top of the draw_osd() function. This change has been updated in the code above.

However, I still have the original issue of stale copies when moving or resizing the geometry. I should only ever have one instance of QTextEdit, as only one is created in the MainWindow constructor and it is never destroyed. Could there be an issue with QTextEdit itself that is preventing me from moving it? As suggested by @Scab, I will try this with QLabel and see if it is an improvement.

Update 6/12/2017 #2:
I modified this code to work with QLabels instead of a QTextEdit. However I get the exact same issue when I go to change the geometry, and now I have two rendered QLabels instead of one.

Update 6/12/2017 #3:
Success! Well, sort of. I have been developing this within an embedded device, so I decided to see what happens when I test it in Qt Creator. Lo and behold, it works! This is good news since it does prove my code is working as intended. I can focus my efforts towards debugging the Linux framebuffer now. Thank you all who offered their help.

TL;DR

How can I change the geometry of a QTextEdit object without having previous versions also rendered?

Answer Source

How can I change the geometry of a QTextEdit object without having previous versions also rendered?

setGeometry is fine , but you should call QTextEdit::clear() to clear the previous data displayed in yourQTextEdit before writing a new text.

If all I want to do is to show some text

Then you might also find interesting to have a look at QLabel.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download