简体   繁体   中英

How to get UI objects to only appear in dialog AFTER button is clicked

  1. I have a dialog that initially has several buttons, let's call them Write, View, OK, and Cancel.
  2. The way it should to is to have the dialog upon creation only have those three buttons and nothing more.
  3. When the Write button is cancelled, it's supposed to create a QLineEdit object in the window above the buttons where the user can enter a new string,which when OK is then clicked will be added to an external QStringList .
  4. When View is clicked, LineEdit should go away (if it's up) and a QListView to come up instead to view everything in that list.
  5. The problem is, I know how to use hide() to get objects that are already in the dialog to NOT appear.
  6. but I am having trouble figuring out how to get an object not currently on the table to appear. I'm new to using Qt so it may be something easy I'm just accidentally overlooking (in fact I hope it is).

    Could anyone please offer advice? Thanks!

Just create the items normally and then set:

ui->control->setVisible(false);

after you have created the UI (after ui->setupUi(this); ) possibly in the constructor (in case you use code generated by Qt Creator).

And when you need them:

ui->control->setVisible(true);

Doc for this:

http://qt-project.org/doc/qt-4.8/qwidget.html#visible-prop

when using a QListView you should also have a QListModel that provides the data to it, if you only have QStrings then a QStringListModel is premade for you to use

to add a row you can do:

int rows = model->rowCount();
model->addRow(rows,1);
QModelIndex index = model->index(rows,0);
model->setData(index, string);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM