Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

creating list with variable number of entries



  • Hi all -

    It turns out that I need to give the user a dialog that enables him to enter a small number (single digits) of IP addresses. The exact number is up to the user.

    Never having actually done this before, the first thing that came to mind was something like a box in which the user could put exactly 1 IP address. To the right of the box would be a "+" sign, which, when pressed, would cause another box and "+" to appear below. The dialog would have commit and cancel buttons as well, of course.

    1. opinions on this?
    2. which Qt GUI construct might be best for this? I've looked through the "long" list, and don't see anything that provided quite the degree of automation I hoped for.

    ADDED: it's probably worth pointing out that this need not be model data; a simple QList will be adequate.

    Thanks...


  • Lifetime Qt Champion

    Hi,

    A QListWidget with a + button at bottom comes to mind for that.

    Add a custom QStyledItemDelegate that returns a QLineEdit with a validator to ensure the content is a valid IP address.

    An alternative is to have a QLineEdit on top of the QListWidget with an Add button beside and when the button is clicked clear the line edit after having added its content to the QListWidget.

    Don't forget to add a - button to delete entries from the list widget.



  • @SGaist thanks for the suggestion. I've created the QListWidget in Designer, and am attempting to populate it with this slot:

    void Informacast::serversReceived(Message *msg)
    {
        vector<string> ipList;
        vector<string>::iterator it;
        string value;
        QListWidgetItem ip;
    
        ipList = msg->findKeys(MsgTagText[TAG_IC_SERVER]);
        ui->listWidgetServers->clear();
        for (it = ipList.begin(); it != ipList.end(); ++it)
        {
            value = msg->getValue(*it);
            ip.setText(QString::fromStdString(value));
            ui->listWidgetServers->addItem(&ip);
        }
    }
    

    Stepping through the debugger, everything looks OK to me, but nothing displays. Do I need to forcibly update the display? I don't see how I would be blocking the event loop.

    Also, you mentioned buttons -- are you talking about regular push buttons?

    Thanks...


  • Lifetime Qt Champion

    @mzimmers said in creating list with variable number of entries:

    QListWidgetItem ip;

    This only exists inside serversReceived()!
    It can't work like that.
    You need to create one QListWidgetItem instance on the heap for each entry in ui->listWidgetServers.



  • @jsulm ahh...I had assumed that the QListWidget actually contained a list (in the C++ STL sense), so I didn't think I needed to keep a separate copy of the contents. Thanks for the clarification.

    This seems to work a bit better:

    void Informacast::serversReceived(Message *msg)
    {
        list<string>::iterator it;
        QString value;
    
        ui->listWidgetServers->clear();
    
        // get the aaddress from the message; store in a STL list.
        m_ipList = msg->findKeys(MsgTagText[TAG_IC_SERVER]);
    
        // for each element in the list
        for (it = m_ipList.begin(); it != m_ipList.end(); ++it)
        {
            //get the value(the IP address) from the key.
            value = QString::fromStdString(msg->getValue(*it));
    
            // add the element to the QListWidget.
            ui->listWidgetServers->addItem(value);
        }
    }
    


  • The code above wasn't storing the correct information in my member list, so I've modified it:

    vector<QListWidgetItem> m_ipList;
    
    void Informacast::serversReceived(Message *msg)
    {
        list<string> keys;
        list<string>::iterator it;
        QString value;
    
      
        ui->listWidgetServers->clear();
    
        // get the address from the message; store in a STL list.
        keys = msg->findKeys(MsgTagText[TAG_IC_SERVER]);
    
        // for each element in the list
        for (it = keys.begin(); it != keys.end(); ++it)
        {
            //get the value(the IP address) from the key.
            value = QString::fromStdString(msg->getValue(*it));
    
            // add the element to our list of IPs.
            m_ipList.push_back(QListWidgetItem(value));
          
            // add the element to the QListWidget.
            ui->listWidgetServers->addItem(value);
        }
    }
    

    So, do I correctly infer that, because the QListWidget doesn't use a model, it's up to me to update my data that is reflected in the list? For example, when the user adds, edits or deletes a row in the list, I need to maintain my list of items as well as the QListWidget?

    Also, to SGaist: I definitely do want to validate the entries, but it's not clear how to use the QStyledItemDelegate if I'm not using a model.


  • Lifetime Qt Champion

    QListWidget has an internal model.

    You can get its content using the item method and then get the string from item.

    The custom delegate in this case should only implement the createEditor method where you will return the customized QLineEdit.



  • @SGaist said in creating list with variable number of entries:

    QListWidget has an internal model.

    You can get its content using the item method and then get the string from item.

    OK, this sounds like I don't need to maintain my own list then, correct? So, the list does actually contain a "real" list of information about its contents?

    The custom delegate in this case should only implement the createEditor method where you will return the customized QLineEdit.

    I guess I need to read more on delegates. The way I've currently implemented it, when the list emits a itemClicked() signal, I call openPersistentEditor(). Is this not the preferred way of doing this?

    I already have a validator for IP addresses:

    class IpValidator : public QValidator
    {
    public:
        IpValidator(QObject *parent = nullptr) : QValidator(parent) {}
        QValidator::State validate(QString &input, int &pos) const;
    };
    

    So, do I need to extract the text from the selected item, set the validator on it, and then call createEditor()?

    Thanks...


  • Lifetime Qt Champion

    No, you don't have to maintain a copy.

    In the createEditor function, you just create a QLineEdit, your validator that you apply to your QLineEdit and then return it.

    The loading of the editor content is done for you automatically later on.



  • @SGaist so, I subclass QItemDelegate and override the createEditor() method? What do I specify as the model?

    Thanks...


  • Lifetime Qt Champion

    No, QStyledItemDelegate.

    The item delegate is applied on the view.



  • @SGaist OK, I looked at the StarDelegate example. I don't really know what I'm doing, but I created this:

    class ICdelegate : public QStyledItemDelegate
    {
        Q_OBJECT
    private:
    public:
        QLineEdit *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                              const QModelIndex &index) const override
        {
            static QLineEdit qle;
            const IpValidator *ipValidator;
            qle.setValidator(ipValidator);
    
            return const_cast<QLineEdit *>(&qle);
        }
    };
    

    I get a segfault on the call to setValidator(). I'm sure I'm overlooking something obvious (I know I'm not using any of the arguments passed to the function), but I can't see what it is.

    Thanks...


  • Lifetime Qt Champion

    @mzimmers said in creating list with variable number of entries:

    I get a segfault on the call to setValidator()

    Of course you do as you're passing a dangling pointer - your ipValidator is not pointing to an IpValidator instance.


  • Lifetime Qt Champion

    Hi

     QLineEdit *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                              const QModelIndex &index) const override
        {
            static QLineEdit qle; // not good idea as i think its deleted for you.. so new it
            const IpValidator *ipValidator; // this needs to be newed too
            qle.setValidator(ipValidator);
    
            return const_cast<QLineEdit *>(&qle);
        }
    


  • @mrjj do I correctly understand that using "new" to create Qt objects here, without a corresponding "delete," will not cause a memory leak?

    In any event, I made the fixes suggested, and the program runs now. A couple of issues remain, though:

    1. the QLineEdit shows up in its own window
    2. the widget from which the QLineEdit is created retains control (I can't access the QLineEdit window until the main widget is closed). I'm guessing I can fix this with a call to activateWindow(), but I really don't want a separate window for this edit in the first place. I was hoping for "in-place" editing -- is this possible with a QListWidget?

    Thanks for the help.


  • Lifetime Qt Champion

    @mzimmers

    Hi , yes normally a new without delete is a leak but the design for the delegate is that you create editor and
    the View deletes it when finished editing.

    1. you dont assign the parent when you new it
      QLineEdit *ql = new QLineEdit(parent)
      Any widget without a parent becomes a window.

    2. fix 1 and 2 is non issue i think :)



  • @mrjj Oh, that's a thing of beauty. Thanks for the reminder on that.

    And you're correct, adding the parent to the creation of the QLineEdit eliminated the new window.

    This issue is mostly fixed now, but there's still a bit of tuning I need to do:

    1. When editing a line, pressing the enter key exits the window. This is logical, but probably not what a lot of users expect. I need to cause it to exit the editor (I think). I think I could fix this by overriding the EditorEvent() method, but I don't know what I'd furnish for the model argument.
    2. It appears that the editor also remains open when the user clicks on a different line. Basically, the same issue as above (I think).

    Thanks again...


  • Lifetime Qt Champion

    @mzimmers
    Hi
    I don't recall pressing enter would do anything but finish editing so not sure all code is as it should be.
    Can you check your code against
    https://stackoverflow.com/questions/26614678/validating-user-input-in-a-qtableview
    As this is the same you want to do, using a Validator.
    It don't matters its other type of view. The delegate is the same.



  • My code had some minor differences, but I changed them to match the example, and the behavior is the same.

    I did realize something, though -- I still had my itemClicked() signal calling a slot that used openPersistentEditor(). I'm guessing that this wasn't what I wanted. But when I disabled this, I get no editor at all. Here's where I use the delegate:

        ICdelegate *icDelegate = new ICdelegate;
        ui->listWidgetServers->setItemDelegate(icDelegate);
    

    Does this look OK to you?
    EDIT:
    PS: I didn't mention that I took SGaist's suggestion above to mean that the only method I overrode was createEditor() -- is this correct?
    UPDATE:
    here's my slot for when the item is double-clicked:

    connect(ui->listWidgetServers, &QListWidget::itemDoubleClicked, this, &Informacast::editItem);
    void Informacast::editItem(QListWidgetItem *item)
    {
        Qt::ItemFlags flags;
        flags = item->flags();
        flags |= Qt::ItemIsEditable;
        item->setFlags(Qt::ItemIsEditable);
        ui->listWidgetServers->editItem(item);
    }
    

    Still not editable, though the coloring changes:
    icast.PNG
    I feel that I'm close to getting this to work, but am overlooking something.


  • Lifetime Qt Champion

    AFAIR, items are editable by defaults.

    Did you set the edit triggers ?



  • @SGaist I hate to disagree with you (mostly because you're always right), but here's my slot:

    void Informacast::editItem(QListWidgetItem *item)
    {
        Qt::ItemFlags flags;
        flags = item->flags();
        if ((flags & Qt::ItemIsEditable) == 0)
        {
            flags |= Qt::ItemIsEditable;
        }
        item->setFlags(Qt::ItemIsEditable);
        ui->listWidgetServers->setEditTriggers(QAbstractItemView::DoubleClicked);
        ui->listWidgetServers->editItem(item);
    }
    

    When I step through this in the debugger, I hit the line where I assign the editable flag.

    To answer your question: no, I hadn't set the edit triggers. But now I am, and the behavior is still the same. In looking at the EditTrigger enums, I don't see another one that I need to set; am I overlooking something?

    EDIT: from a debugging session, it appears that the DoubleClicked was already set.



  • @mzimmers said in creating list with variable number of entries:

    item->setFlags(Qt::ItemIsEditable);

    This resets all your flags, to editable only. Yet you earlier picked up the existing flags. I have a feeling you intended

    item->setFlags(flags);
    

    to enable editability? I have no idea whether this is relevant to your issue, it's just a code observation.



  • @JonB ding ding ding we have a winner! Good catch there. I was going to ask whether the setFlags worked that way, but I got distracted by all my other mistakes.

    Now, the item is editable, but...my validator doesn't seem to be taking effect. I think I know why, but I'm not sure what to do about it. Here's my class:

    class ICdelegate : public QStyledItemDelegate
    {
        Q_OBJECT
    private:
    public:
        explicit ICdelegate(QObject *parent = nullptr) :
            QStyledItemDelegate(parent)
        {
        }
        QLineEdit *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                              const QModelIndex &index) const override
        {
            // some junk code to disable compiler warnings about unused variables.
            const QModelIndex xxx(index);
            if (xxx == index)
            {
                ;
            }
            const QStyleOptionViewItem yyy(option);
    
            QLineEdit *qle;
            qle = new QLineEdit(parent);
            qle->setValidator(new IpValidator);
    
            return (qle);
        }
    };
    

    Am I doing something wrong with qle? I tried making it a member variable, but got a compiler error.

    Thanks...


  • Lifetime Qt Champion

    Try with:

    
    qle->setValidator(new IpValidator(qle));
    


  • @SGaist another winner! The validator is now invoked. So, do I understand this to mean that the call to setValidator isn't sufficient to associate the QLineEdit with its validator; the validator uses its parent as the basis for what to operate on?

    As a side benefit, pressing "enter" no longer dismisses the dialog. I think this problem is solved (but I'm not going to so mark it quite yet).

    Thanks to everyone who helped with this.



  • @mzimmers
    I'm waiting for The Master's answer on this too :) I looked up the docs and I do not see where it indicates this is required, and OK your validator may leak but I don't see it will die...


Log in to reply