How to update Qt-GUI form view with implemented code?

Viewed 212

For demonstration I created a simple simple widget in Qt-Creator.

  • widget.h
  • widget.cpp
  • main.cpp
  • widget.ui

  1. I added a QPushButton to Design-Form by drag-and-drop

  1. Implementing QFont expression to widget.cpp:
// widget.cpp
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) 
{

      ui->setupUi(this);

      QFont serifFont("Arial", 20, QFont::Bold); // Added QFont expression
      ui->pushButton->setFont(serifFont); 
      ui->pushButton->setStyleSheet("color: blue"); // and text in blue color
    }

    Widget::~Widget() { delete ui; }

  1. Clicking on "build" and "run" (Play Button) the widget appears with blue Text in Arial, 20, bold widget with blue text:

widget with blue text


  1. But in the Qt GUI design form, the pushButton text is still black and default, does not appear updated gui designer black text:

gui designer black text


Is there any way to synchronize the implemented code with the GUI-form?
Like saying to Qt: Update widget.ui according to source code files


A not proper workaround is:

  • Only using GUI Designer form (no direct code implementing): Selecting pushButton -> Adjust font Properties in property manager (Qt updates XML file, by markup language)

2 Answers

No, there is no way. The Qt Designer preview only renders the GUI built based on the .ui.

The Qt Designer is a one-way tool. It is not able to parse changes you make in your code back into the UI design file.

Related