what Widget / tools should I use to create this type of list?

  • I need to create a list of Qstrings that I read in from a file. I then need to be able to click each one and have an "on Clicked" slot that I can use to do something with the text. The last part is that I need to have a little clickable "x" or icon next to each list item that does something different with that text.

    My idea is to create a widget with a clickable text and a small button at the end of the x and just add them to a page as i read in each line of the text file. However I have no idea how to even start this.

    I'm decent with c++ but need to get something up and running in qt as fast as I can. Any tips, pointers, or suggestions are welcome!

  • Lifetime Qt Champion

    Hi and welcome
    you could use a QListWidget and use the
    void QListWidget::setItemWidget ( QListWidgetItem * item, QWidget * widget )
    to insert the button.

    This becomes very heavy if you have many rows, but should qualify as up and running fast.

    If you right click on the QListWidget you can add a slot for "on Clicked"

    for the button, you have the issue that it has no information on the selected row, but you could cheat and just
    QListWidgetItem * QListWidget::currentItem() const
    in the "On click" function of the button.

    you would create the buttons on the go so would have to hook up its "Clicked"
    ala (main window)

    QPushButton*   m_button = new QPushButton ( "X", this );
    bool c = connect ( m_button, SIGNAL ( released() ), this, SLOT ( buttonPressed() ) );
    void MainWindow::buttonPressed()

    Just a fast suggestion.many ways to do this :)

  • @mrjj thank you!!! You are the first person to point me in at least some kind of direction. Can I ask how I would get the x button to appear next to each list item?

  • Lifetime Qt Champion

    @mrjj said:
    Sad I never found a way to place it to the right as I wanted. On same line as the text.

    Also, this has a flaw. If user Click directly on the button, the row is not selected since the button got the focus.
    So I inherited a Button and added a variable to it , so i can know which row (index) a button belonged to.

    How many lines do you got ?

  • @mrjj Wait you have this working? I'm still trying to figure it out haha. Its going to be dynamic. Every time you start the application its going to read from text file that has on average 10-20 lines of text (short lines, 24 char max). If i could just get the text clickable I would be happy. I'm having trouble just adding text to it.

  • Lifetime Qt Champion

    @mrjj said:

    You create QListWidgetItem using new and insert into list. List owns them and will delete.
    The text as such is not clickable as such. its the whole row/QListWidgetItem.

    If you use the visual editor, you can just right click the List and "Goto Slot" and select the
    "Clicked" slot.
    This gives you a function that get called when clicking on Item.

    So you have some lines you load into a list and the user can do something to the text by clicking the text or clicking the button to do something different?

  • Lifetime Qt Champion

    Something like this could work, using a place holder for a label and a button
    will get you this
    you still need to hook up button&label for it to do something :)

    #include "mainwindow.h"
    #include <QApplication>
    #include <QCheckBox>
    #include <QRadioButton>
    #include <QPushButton>
    #include <QVBoxLayout>
    #include <QLabel>
    #include <QFile>
    #include <QMessageBox>
    #include <QTextStream>
    void CreateItem (  QListWidget * TheList , QString& TheText  )
        QListWidgetItem * item = new QListWidgetItem ( "" );
        TheList->addItem ( item );
        // add a place hodler for label and button
        QWidget * w = new QWidget();
        w->setLayout ( new QHBoxLayout() );
        QPushButton *but = new QPushButton ( "Do it" );
        QLabel *lab = new QLabel ( TheText );
        // make row a bit bigger
        item->setSizeHint ( QSize ( item->sizeHint().width(), 30 ) );
        // add the label and button to the layout
        w->layout()->addWidget ( lab );
        w->layout()->addWidget ( but );
        // reduce the space around it abit
        w->layout()->setContentsMargins ( 1, 1, 1, 1 );
        // set this combined widget for the row
        TheList->setItemWidget ( item, w );
    int main ( int argc, char ** argv )
        QApplication app ( argc, argv );
        QListWidget * list = new QListWidget();
        QFile file ( "e:/mylist.txt" );
        if ( !file.open ( QIODevice::ReadOnly ) ) {
            QMessageBox::information ( 0, "error", file.errorString() );
        QTextStream in ( &file );
        while ( !in.atEnd() ) {
            QString line = in.readLine();
            CreateItem ( list, line );
        return app.exec();

Log in to reply