Which Qt object to print out a long list?



  • Okay, so struggling to find a way to effectively teach myself Qt but I’m not giving up.

    What I need to do is print out a looooong list of data in a text window (within the main screen of the object) with scroll bars.
    Even better if I am a able to select lines of data.

    Can anyone suggest such an object?

    Thanks.

    Uberlinc.


  • Moderators

    @Uberlinc said in Which Qt object to print out a long list?:

    Okay, so struggling to find a way to effectively teach myself Qt but I’m not giving up.

    My personal approach is to use Qt to create a program that does something I want.

    What I need to do is print out a looooong list of data in a text window (within the main screen of the object) with scroll bars.
    Even better if I am a able to select lines of data.

    Can anyone suggest such an object?

    Some extra info would be useful:

    1. Are you programming in C++ or QML?
    2. What is your data format currently in? Just a single string, or an array of some kind?
    3. What kind of selection do you want? The ability to highlight text (like in a text editor), or the ability to select individual lines?

    Anyway, I'll take a stab: If you're using C++ and want a text editor type of view, use a QTextEdit or QPlainTextEdit. If you're using C++ but not after a text editor, put your text in a QStringListModel and display the model with a QListView.



  • Hi,

    Yes, I have a program in mind. This is why I am looking for this specific object type.

    To answer your questions:

    1. I’m coding in C++
    2. The Data is currently in a vector of a class containing several integers and a date field.
    3. The object that I wish to select is the date.

    I want to be able to highlight at least 2 lines (i.e. date 1 and date 2) and then do calculations on the data in between.
    This particular part isn’t overly important - I know that there are “date spinner” objects to use instead. It’s more just a cool way to do it.

    First things first, I just want to print out the list in a text box and be able to scroll through it like a long document.

    Many thanks.

    Uberlinc


  • Lifetime Qt Champion

    Hi,

    Wouldn't that be covered by either a QListView or a QTableView and a custom model wrapping your data structure ?


  • Moderators

    @Uberlinc said in Which Qt object to print out a long list?:

    1. The Data is currently in a vector of a class containing several integers and a date field.
    2. The object that I wish to select is the date.

    I want to be able to highlight at least 2 lines (i.e. date 1 and date 2) and then do calculations on the data in between.
    This particular part isn’t overly important - I know that there are “date spinner” objects to use instead. It’s more just a cool way to do it.

    First things first, I just want to print out the list in a text box and be able to scroll through it like a long document.

    First, convert your data to an array of strings:

    QStringList lineList;
    QVector<MyData*> vector = ...
    for (MyData* datum : vector)
    {
        QString thisLine = datum->date().toString(Qt::ISODate); // Assuming your data is in a QDate object
        thisLine += '\t' + QString::number(datum->int1()) + '\t' + QString::number(datum->int2());
    
        lineList << thisLine;
    }
    

    Now, there are a few things you can try to display your data. You mentioned you want to learn Qt so I'll describe 3, even though the 1st one doesn't meet all your requirements:

    1. Concatenate your array into one long, newline-delimited string (linelist.join('\n')) and display it in a QTextEdit/QPlainTextEdit. This is the easiest route, and it lets you scroll through your lines. However, it doesn't let you select two lines and do in-between calculations.

    2. Use a model-view approach to allow selection of 2 lines. The fastest way to get started with model-view is to put your lineList in a QStringListModel, and display that model with a QListView

    3. Forget about the strings, and create a custom model for your data class by subclassing QAbstractTableModel, and display it in a QTableView. This route takes the most effort, but gives you the most flexibility. See https://doc.qt.io/qt-5/model-view-programming.html and https://doc.qt.io/qt-5/modelview.html to get started.

    Finally, search the Qt documentation for the classes that @SGaist and myself mentioned above. Qt docs are very comprehensive; I recommend you familiarize yourself with them:

    • QDate
    • QString
    • QStringList
    • QTextEdit
    • QPlainTextEdit
    • QStringListModel
    • QAbstractTableModel
    • QListView
    • QTableView


  • That's very comprehensive!
    Thank you very much.

    I'll go away in try your suggestion.

    Many thanks for your help!

    Regards,

    Uberlinc



  • I tried a small QListView and the compiler spat because it couldn’t find #include <QListView>
    Do I need to change a setting to get it to find this library?

    Thanks.

    Uberlinc.


  • Moderators

    @Uberlinc Do you have QT += widgets in your pro file?



  • Yes, thank you - that worked.
    I’ve been following:
    http://toto-share.com/2012/02/qt-qlistview-tutorial/
    which gives a good, simple example that I can experiment with.

    The problem I’ve found is it’s a wonky sized window.
    I’ve tried adding objects like setGeometry() but they all spit an error.

    An someone tell me how to set minimum sizes of window?
    Also, how to set the size or proportions to main window of the QListView object itself?

    Thanks.


  • Qt Champions 2016

    @Uberlinc
    Hi
    The ListViewExample lve IS the windows it self.
    When created without a parent, any widgets will have windows flags set and become a the window itself.
    There is no mainwindow involved here.
    Only the Widget.

    you can say
    lve.resize(w,h);
    before show() to make it bigger.


Log in to reply
 

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