PDA

View Full Version : setItemWidget in QWidgetList does not work



Quabla
14th March 2013, 21:47
Hey

i want to create a list of widges. each item should have a label and three buttons (like my SSH_list_item). i tried it like this:




void sshfsGUI::create_new_entry(QString name, QString mountpoint, QString address){
SSH_list_item* list_item = new SSH_list_item(name, mountpoint, address, this);
QListWidgetItem* item = new QListWidgetItem(name);
ui->SSH_List->addItem(name);
ui->SSH_List->setItemWidget(item, list_item);
list_item->show();
}


the class looks like this:



class SSH_list_item : public QDialog
{
Q_OBJECT

public:
explicit SSH_list_item(QWidget *parent = 0);
SSH_list_item(QString _name, QString _mountpoint, QString _address, QWidget *parent=0);
~SSH_list_item();

private:
Ui::SSH_list_item *ui;
QString name, mountpoint, address;
};


but what i get is a list of strings and the widget as a separate window. when i leave out " list_item->show();" there is only the list of strings.
here is a screenshot: http://imageshack.us/photo/my-images/109/screenshotro.png/
what am i doing wrong?

wysota
14th March 2013, 22:44
Your "item" has no relation to the list widget. You are adding a string to the list which effectively creates a new item independent of your "item" object.

Quabla
14th March 2013, 22:58
thank you i changed it to


void sshfsGUI::create_new_entry(QString name,QString mountpoint ,QString address){
SSH_list_item* list_item = new SSH_list_item(name, mountpoint, address, this);
QListWidgetItem* item = new QListWidgetItem(name);
ui->SSH_List->addItem(item);
ui->SSH_List->setItemWidget(item, list_item);
list_item->show();
}


now there is only the string list. the seperate windows do not appear anymore

edit: ok the mistake is in the order. i have to show the widget before adding it, like this:


void sshfsGUI::create_new_entry(QString name,QString mountpoint ,QString address){
SSH_list_item* list_item = new SSH_list_item(name, mountpoint, address, this);
QListWidgetItem* item = new QListWidgetItem(name);
list_item->show();
ui->SSH_List->addItem(item);
ui->SSH_List->setItemWidget(item, list_item);
}

but there are new issues: only the widget of the last SSH_list_item added is visible and the name of it appears to times. and when i change the size og the main widget, the SSH_list_item widget disappears and again all i see is the string list.
sycreenshot: http://imageshack.us/photo/my-images/580/screenshotpso.png/

wysota
15th March 2013, 06:10
Your problem has to be elsewhere. The code you posted looks fine.