QCombobox in table widget



  • Hello i have a qcombobox in qtablewidget, i need to get the row (of the tablewidget, not of the combo box) when i click on the combobox but i not able to do it, can you hel me please?

    Thanks



  • Hi,

    the combo box does not know anything about the table widget. As you are the one, creating and positioning it, store it somewhere.



  • I don't know what you want to do, but often it is better to use a "QStyledItemDelegate ":http://doc.qt.nokia.com/latest/qstyleditemdelegate.html for these kind of things. I have written a wiki article "Combo Boxes in Item Views":http://developer.qt.nokia.com/wiki/Combo_Boxes_in_Item_Views that demonstrates the principles.



  • Hi
    I have write this:

    @
    QComboBox *combo;
    for (int i=0;i<ui->tableWidget->rowCount();i++){
    combo = new QComboBox;
    combo->addItems(lista_combo);
    combo->setObjectName("combo"+QString::number(i));
    connect(combo,SIGNAL(currentIndexChanged(int)),this,SLOT(metto_stringa(int)));
    ui->tableWidget->setCellWidget(i,4,combo);
    }
    ui->tableWidget->resizeColumnsToContents();
    @

    there is a way to pass the combo name so i can get the row number?

    [EDIT: code formatting, please wrap in @-tags, Volker]



  • You can get the sender object by calling "sender() ":http://doc.qt.nokia.com/latest/qobject.html#sender, it returns the sender of the signal that caused the calling of this slot (if there is any). If the slot was not called via signal/slot connection (eg. you can call every slot directly from your code), the method returns a null pointer. So check it!

    In you slot you can do something like this:
    @
    void myClass:fancySlot()
    {
    QObject *s = sender();
    QString senderName;
    if(s)
    senderName = s->objectName();

    // check for set sender name her and process further
    

    }
    @

    But do you really need all the combo boxes in the table at the same time? Or do you need them only to change a value if the user wants to edit the data? If you need the latter, you're better off with the item delegate approach mentioned in my former comment.



  • Thanks it works,
    I really need all this combo

    Many Thanks

    Luca


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.