Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Which Qt object to print out a long list?
QtWS25 Last Chance

Which Qt object to print out a long list?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 5 Posters 4.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    Uberlinc
    wrote on last edited by
    #1

    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.

    JKSHJ 1 Reply Last reply
    0
    • U Uberlinc

      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.

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      @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.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      3
      • U Offline
        U Offline
        Uberlinc
        wrote on last edited by
        #3

        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

        JKSHJ 1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • U Uberlinc

            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

            JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by JKSH
            #5

            @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

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            1 Reply Last reply
            4
            • U Offline
              U Offline
              Uberlinc
              wrote on last edited by
              #6

              That's very comprehensive!
              Thank you very much.

              I'll go away in try your suggestion.

              Many thanks for your help!

              Regards,

              Uberlinc

              1 Reply Last reply
              0
              • U Offline
                U Offline
                Uberlinc
                wrote on last edited by
                #7

                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.

                jsulmJ 1 Reply Last reply
                0
                • U 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.

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

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

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  2
                  • U Offline
                    U Offline
                    Uberlinc
                    wrote on last edited by
                    #9

                    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.

                    mrjjM 1 Reply Last reply
                    0
                    • U Uberlinc

                      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.

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by mrjj
                      #10

                      @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.

                      1 Reply Last reply
                      1

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved