Qt: Advice about dynamic UI [closed]
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
开发者_运维百科 Improve this questionI'm trying to make an UI that contains a list of such items:
http://img707.imageshack.us/img707/1114/16900293.png http://img707.imageshack.us/img707/1114/16900293.png http://img707.imageshack.us/img707/1114/16900293.pngUser can change the count of the items, so the UI is dynamic.
What are the best components to get user input, like in the picture? Is it multiple QLabel
s and QLineEdit
s?
If the UI contains a lots of these items you could consider using QTableView (or QTableWidget). You could present the static parts "x=", "(2n" and "t)" in static columns or you could create a delegate which would draw the static parts with different color.
The advantage in using the QTableView instead of QLineEdits is that moving with keyboard is easy. By using the cursor keys you can easily move the focus around. The editing experience is the same as in spreadsheet applications like Excel.
I have done something similar with several QQueue containers full of QLabel and QLineEdit items. Just append and remove items when the user desires more or less. Could combine that with a QGridLayout and put things where you need. Not sure if that's the best way, but works well for me.
http://doc.trolltech.com/4.4/qqueue.html
QQueue<QLineEdit *> linedit;
QGridLayout *gridboxLayout;
gridboxLayout= new QGridLayout();
linedit.enqueue(new QLineEdit ());
gridboxLayout->addWidget(linedit.last(),row,column);
this->setLayout(gridboxLayout);
You are gonna need to add stuff for keeping track of rows, columns and items and such but that is the basic setup. Just keep appending new items to the QQueue and insert into a new row of the gridLayout. You can even reference them by row and delete them later.
The other easy way to do this, which will work with the designer, if you have a reasonable max number of items, is to just build the whole thing and hide the widgets you don't want to see until you need them.
If you have a large number of very similar widgets (like that), I would recommend either investigating one of the model views, or making a custom widget, and letting those widgets handle the editing themselves. Then your main widget can just make a list of custom widgets in a scrolled area, and not worry about keeping track of each value.
精彩评论