1

I would like to create a windows application that displays a window based on a existing text file. For example, I may have a text file with the following information:

window_size 400, 300
push_button 2
radio_button 5

My program should be able to read this text file and create a window of 400 x 300 pixels, with 2 push buttons and 5 radio buttons. Of course, the content of the text file may change and is unpredictable. Please ignore at this moment other nessesary data, like the position and size of the buttons, I just need to know if its possible to do this, and a general idea on how to do it.

I'm using Qt with C++. I may change Qt if there is a better option to do this, but I must stick with C++.

user3787097
  • 181
  • 3
  • 14

2 Answers2

2

You should parse and set accordingly.

Create a QWidget, parse the first line of the text file, set its size. Then parse next lines to get the number of buttons, create widgets dynamically. Something like:

// main
// ...

QString geoLine = stream.readLine();
int width = geoLine.split(" ")[1].toInt();
int height = geoLine.split(" ")[2].toInt();

QWidget * widget = new QWidget();
widget->setGeometry(0, 0, width, height);

// create a layout for the child widgets, then create and add them dynamically.    
QVBoxLayout * layout = new QVBoxLayout();

int nButtons = stream.readLine().split(" ")[1];

// full control of dynamically created objects
QList<QPushButton *> buttons;

while(nButtons > 0) {
QPushButton * button = new QPushButton();
buttons.append(button);
layout.addWidget(button);
nButtons--;
}

// same for radio buttons
// ...

widget->setLayout(layout);
widget->show();

// ... etc, app exec,
qDeleteAll(buttons);
delete widget;
return 0;

If you want to learn the widget type from push_button or radio_button directives; you must switch - case onto those parsed strings.

There is also completely another way. You can create a form (.ui) file using an XML data. You must create a ui class (like the template designer form class created by Qt), and create its .ui file according to your text file - parsed and converted to a proper XML.

As far as I know Qt handles the widget creations using that XML information, and generates the file ui_YourClass.h..

baci
  • 2,528
  • 15
  • 28
1

As canberk said you can use native Qt UI file format .ui by usage of QUiLoader class - see reference http://doc-snapshot.qt-project.org/qt5-5.4/quiloader.html

Dawid Komorowski
  • 538
  • 1
  • 6
  • 15