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. QAbstractItemModel with QTableView
QtWS25 Last Chance

QAbstractItemModel with QTableView

Scheduled Pinned Locked Moved General and Desktop
12 Posts 3 Posters 3.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.
  • T Offline
    T Offline
    tokafr
    wrote on last edited by
    #1

    Hello again.

    I created a model class which inherits QAbstractItemModel, I have a list of data there. and I also have a tableView. I want the data to be displayed on the view, exactly the same items as in model, not an exstra rows or columns.
    I think I have a problem with model's rowCount() and columnCount() methods, I cannot set the number of rows and columns correctly. for example I have 3 objects in data list and I want to have a table with 4 columns, when I add items from view I want the rows to add after it reaches the last column in previous row. how can I solve it? do you have any idea?

    thank you!!!

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Subst27
      wrote on last edited by
      #2

      Hi!

      for your purpose easy use the QAbstractTableModel

      but in both cases when you inherits your own class from Abstract class
      you must

      1. describe some data structure to describe your model, for example

      QVector <QVector <QVariant> > m_data;

      1. reimplement virtual methods such as rowCownt(),columnCount(), data(), setData() appenRows(), removeRows() and so one

      And you yourself must describe policy of your model

      E.g.

      @int MyModel::columnCount()
      {
      if (m_data.size())
      return m_data.at(0).count;
      else
      return 3;
      }@

      @int MyModel::rowCount()
      {
      return m_data.size();
      }@

      I'm correctly understood your question?

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        [quote author="tokafr" date="1421255724"]Hello again.

        I created a model class which inherits QAbstractItemModel, I have a list of data there. and I also have a tableView. I want the data to be displayed on the view, exactly the same items as in model, not an exstra rows or columns.
        I think I have a problem with model's rowCount() and columnCount() methods, I cannot set the number of rows and columns correctly. for example I have 3 objects in data list and I want to have a table with 4 columns, when I add items from view I want the rows to add after it reaches the last column in previous row. how can I solve it? do you have any idea?

        thank you!!![/quote]

        So, you want your items to basically wrap around, like an exporer window does when displaying files as icons right?

        That is easy: that is just displaying a list-type model on a QListView. However, the list view needs to be set in the right mode to display like that. Set it to IconMode and you're in business.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tokafr
          wrote on last edited by
          #4

          thank you Andre, that's exactly what I need. I did it and it works but I can't select items in icon view, what is the problem? and how can I set the size of the item here, with delegate sizeHint()?

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            Selecting should work. Do you have the right selection mode set on the view?

            The size of the items I think (not sure, don't use it often) can be set in several ways. You can use the sizeHintRole from the model, use a delegate, or use the setGridSize method on the QListView.

            If your items are all the same size, it's a good idea to call setUniformItemSizes(true) as well for performance reasons.

            1 Reply Last reply
            0
            • T Offline
              T Offline
              tokafr
              wrote on last edited by
              #6

              oh sorry selection works but without setting the color to the icon frame
              but i do want it to set color.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                So, you want to modify how selection is displayed? That will require a style sheet, a delegate or a proxy style.Or, setting the palette could also work, but then you'd have to use a QItemDelegate instead of a QStyledItemDelegate on your item view.

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  tokafr
                  wrote on last edited by
                  #8

                  ok I did it, but now I have another question :) when I change window size I want the items accept it, they have to change their positions.

                  can I do it?

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #9

                    Apply layouts.

                    1 Reply Last reply
                    0
                    • T Offline
                      T Offline
                      tokafr
                      wrote on last edited by
                      #10

                      How!! :D can you point me, is there any method of QListView?

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        andre
                        wrote on last edited by
                        #11

                        No. The position of a QListView in a larger setting is not a property of QListView. Just lookup the word "layout" in your help system, and you'll get plenty of references.

                        1 Reply Last reply
                        0
                        • T Offline
                          T Offline
                          tokafr
                          wrote on last edited by
                          #12

                          ok thank you!!

                          1 Reply Last reply
                          0

                          • Login

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