2 years ago
#26196
myk
QLabel::setText() writes on top of previously displayed text
QLabel::setText overwrites the previously displayed text when the text is updated:
QLabels above are added on a form in Qt designer and then added onto one tab of a QTabWidget at runtime. The text overwrite is not visible when the the tab / form is first displayed, but when navigating back from another tab or just updating the label values.
Result is consistent on MacOs and Linux. Have not tried on Windows.
Have tried using QLabel::clear() and QLabel::repaint() before QLabel::setText() without success.
Have tried producing a minimal code example without success.
Any thoughts on what causes this effect?
Have assembled an example, the mistake is highlighted.
overwrite.pro:
QT += widgets
HEADERS = mainwindow.h
SOURCES = main.cpp \
mainwindow.cpp
FORMS += \
form.ui
mainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLabel>
#include <QTabWidget>
#include "ui_form.h"
namespace Ui {
class Form;
}
class formWidget : public QWidget
{
Q_OBJECT
public:
explicit formWidget(QWidget *parent = nullptr);
~formWidget() {delete ui;};
void setLabel(QString labelText);
private:
Ui::Form* ui;
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
private slots:
void tabChanged(int CurrentTab);
private:
QTabWidget* tabs = nullptr;
QLabel *label = nullptr;
QWidget* tab1;
QGridLayout* tab1Layout = nullptr;
formWidget* chartForm = nullptr;
};
#endif // MAINWINDOW_H
main.cpp
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow window;
window.show();
return app.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QLayout>
formWidget::formWidget(QWidget *parent):
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
};
void formWidget::setLabel(QString labelText){
this->ui->label->setText(labelText);
}
MainWindow::MainWindow(QWidget *parent): QMainWindow(parent)
{
//Create tab 1 contents
tab1Layout = new QGridLayout;
this->chartForm = new formWidget;
tab1Layout->addWidget(chartForm, 0, 1);
tab1 = new QWidget;
tab1->setLayout(tab1Layout);
//Create the tab 2 contents
this->label = new QLabel;
this->label->setText( "Tab 2 QLabel." );
QHBoxLayout* tab2layout = new QHBoxLayout;
tab2layout->addWidget(label, 1);
QWidget* tab2 = new QWidget;
tab2->setLayout(tab2layout);
//Create the tab widget and add the contents
tabs = new QTabWidget;
tabs->addTab(tab1, QString("tab 1"));
tabs->addTab(tab2, QString("tab 2"));
this->setCentralWidget( tabs );
QObject::connect(tabs, SIGNAL(currentChanged(int)), this, SLOT(tabChanged(int)) );
}
void MainWindow::tabChanged(int currentTab){
if (currentTab == 0) {
chartForm = new formWidget; //Obviously silly
tab1Layout->addWidget(chartForm, 0, 1); //Obviously silly
this->chartForm->setLabel("Overwrite Text");
}
}
form.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>327</width>
<height>232</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Default QLabel Text</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
c++
qt
qlabel
0 Answers
Your Answer