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. QTableWidget or QTableView
Forum Updated to NodeBB v4.3 + New Features

QTableWidget or QTableView

Scheduled Pinned Locked Moved General and Desktop
36 Posts 6 Posters 17.4k Views 1 Watching
  • 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.
  • A Offline
    A Offline
    andre
    wrote on last edited by
    #11

    [quote author="DaFranzl" date="1344346587"]Ok, I have implemented the functions as far as I can. Are you sure u meant
    @QAbstractItemModel* model() const;@
    and not
    @QAbstractTableModel* model() const;@
    ?
    [/quote]
    Yes, I'm sure. The fact that you're using a QAbstractTableModel is an implementation detail. The views all work on QAbstractItemModel anyway. Note that you can return any type of model in your implementation, as they all inherit from QAbstractItemModel anyway.

    [quote]
    If I am right with my guess is this the right implementation?

    @QAbstractTableModel* PLAYERHOLDER::model() const
    {
    return new CONTACTMODEL(this);
    }@

    After running the Code I recieve many errors with this line.

    @mutable CONTACTMODEL *m_model;@

    Missing type specifier - int assumed. Note: 'default-int' isn't supported by C++ and also
    Syntaxerror: Missing ; infront of *

    (The errors are translated from German, they can vary a bit.
    [/quote]

    In principle, this is correct, but I would not create a new CONTACTMODEL every time model() is called. Instead, initialize m_model to 0 in your constructor, and in your implementation, only create a new instance if m_model is 0, which you then assing to m_model. Then, return m_model. That will ensure you only create one model instance per PLAYERHOLDER.

    I don't get why you get the error you get. The line is in the header, inside PLAYERHOLDER, right?

    [quote]
    Also i dont know how to realise the data function, it looks like this at the moment:
    @QVariant PLAYERHOLDER::CONTACTMODEL::data(const QModelIndex &index, int role) const
    {
    if (!index.isValid())
    return QVariant();

    if (index.row() >= m_playerHolder->playerlist.size())
        return QVariant();
    
    if (role == Qt::DisplayRole)
        switch (index.column())
        {
        case 0:
            return m_playerHolder->playerlist.value(index.row())->getName();
        case 1:
            return m_playerHolder->playerlist.value(index.row())->getEmail();
        case 2:
            return m_playerHolder->playerlist.value(index.row())->getHandy();
        case 3:
            return m_playerHolder->playerlist.value(index.row())->getBirthday();
        case 4:
            return m_playerHolder->playerlist.value(index.row())->getPassnumber();
        default:
            return QVariant();
        }
    else
        return QVariant();
    

    }@

    But this can't work if for example the first row player has the id 1 and the second 3 so there is 1 row left.[/quote]

    Well, then I think you'll need to create a mapping, so you map a row number to an id. You can't use them directly. You could, for instance, use QHash::keys() like this:
    @
    QList<int> rowToKeyMapping = m_playerHolder->playerlist.keys();
    if (role == Qt::DisplayRole)
    switch (index.column())
    {
    case 0:
    return m_playerHolder->playerlist.value(rowToKeyMapping[index.row())]->getName();
    //etc.
    @

    Also, you still are missing some checks. You have a good start, but you should also check the column number and if a valid parent index is supplied.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DaFranzl
      wrote on last edited by
      #12

      Because of the error question, this is how my headerfile looks like.
      @#ifndef PLAYERHOLDER_H
      #define PLAYERHOLDER_H

      #include <QtCore>
      #include <player.h>
      #include <datasource.h>

      class PLAYERHOLDER
      {
      public:
      static PLAYERHOLDER* getInstance();
      void createPlayer(PLAYER player);
      void updatePlayer(int id);
      void deletePlayer(int id);
      PLAYER
      findPlayer(int id);
      void loadPlayers(int teamid);

      QAbstractItemModel* model() const;
      

      private:
      PLAYERHOLDER();
      static PLAYERHOLDER thePlayerholder;
      QHash<int, PLAYER
      > playerlist;
      DATASOURCE *datasource;
      mutable CONTACTMODEL *m_model;

      class CONTACTMODEL : public QAbstractTableModel
      {
          Q_OBJECT
      public:
          explicit CONTACTMODEL(PLAYERHOLDER* holder);
      
          int rowCount( const QModelIndex &parent ) const;
          int columnCount( const QModelIndex &parent ) const;
          QVariant data( const QModelIndex &index, int role ) const;
          QVariant headerData( int section, Qt::Orientation orientation, int role ) const;
      
      private:
          PLAYERHOLDER* m_playerHolder;
      };
      

      };

      #endif // PLAYERHOLDER_H
      @

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

        I think the problem is that the compiler doesn't know about CONTACTMODEL yet. Try to reorder your header a bit, or introduce a forward declaration for the CONTACTMODEL.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DaFranzl
          wrote on last edited by
          #14

          Solved :)

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DaFranzl
            wrote on last edited by
            #15

            You said the nested class should have the Q_Object, but the compiler says it is not supported for nested classes

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

              I don't think I said that, but you probably don't need it either. Note that you could even keep the whole declaration of CONTACTMODEL out of the header. Just forward declare it, and put all of it in your cpp file or use a separate header file for it. That way, you can change it however you want and classes including PLAYERMODEL's header won't need to recompile.

              1 Reply Last reply
              0
              • D Offline
                D Offline
                DaFranzl
                wrote on last edited by
                #17

                I think i keep it as nested class sofar, a couple of posts before you said that i need more functions to keep the view updated if something in the playerholder changes. What function would that be?

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

                  The class itself can still be nested, it is just about the place where you declare it. The methods you're going to need to access are the protected beginInsertRows, endInsertRows, beginRemoveRows, and endRemoveRows as well as the itemsChanged signal. These are needed to make the model and the connected views react properly to modifications in your data.

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    DaFranzl
                    wrote on last edited by
                    #19

                    I tried to forward declare the class, but i just failed:( Nerver did forward declaration before and google didnt help. At the moment i just added an update() function to CONTACTMODEL and call it after create,modify,delete functions.
                    @void update()
                    {
                    this.reset();
                    }@

                    and it works:)

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

                      That's a nice hack, but please don't use it in production code. Instead, learn how to properly update your model.

                      1 Reply Last reply
                      0
                      • D Offline
                        D Offline
                        DaFranzl
                        wrote on last edited by
                        #21

                        You talked about the forward declaration before, I think i need use it because nested classes arent allowed to use SLOTS and SIGNALS. Can you explain me how to forward declare?

                        1 Reply Last reply
                        0
                        • D Offline
                          D Offline
                          DaFranzl
                          wrote on last edited by
                          #22

                          Can anybody explain how to forward declar the nested class?

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

                            [quote author="DaFranzl" date="1344438774"]Can anybody explain how to forward declar the nested class?[/quote]

                            Moderators note:
                            Please practise some patience. It is considdered rude to "up" your topic within a day or two, some say a week.

                            1 Reply Last reply
                            0
                            • M Offline
                              M Offline
                              mlong
                              wrote on last edited by
                              #24

                              bq. Can anybody explain how to forward declar the nested class?

                              It's pretty straightforward stuff. Google can point you to a ton of good information. If you need more clarification, we'd be glad to help.

                              Software Engineer
                              My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                              1 Reply Last reply
                              0
                              • D Offline
                                D Offline
                                DaFranzl
                                wrote on last edited by
                                #25

                                Sorry for the up-post, but I googled a lot and visited 2 pages of links but I still dont know how to forward declare a nested class. I already tried this:

                                @
                                #include <QtCore>
                                #include <player.h>
                                #include <datasource.h>
                                #include <contactmodel.h>

                                class PLAYERHOLDER
                                {

                                public:
                                static PLAYERHOLDER* getInstance();
                                void createPlayer(PLAYER player);
                                void updatePlayer(int id);
                                void deletePlayer(int id);
                                PLAYER
                                findPlayer(int id);
                                void loadPlayers(int teamid);

                                QAbstractItemModel* model() ;
                                

                                private:
                                PLAYERHOLDER();
                                static PLAYERHOLDER thePlayerholder;
                                QHash<int, PLAYER
                                > playerlist;
                                DATASOURCE *datasource;
                                mutable CONTACTMODEL *m_model;
                                };
                                @

                                @
                                #include <playerholder.h>

                                class PLAYERHOLDER;

                                class CONTACTMODEL : public QAbstractTableModel
                                {
                                public:
                                explicit CONTACTMODEL(PLAYERHOLDER* holder);

                                    int rowCount( const QModelIndex &parent ) const;
                                    int columnCount( const QModelIndex &parent ) const;
                                    QVariant data( const QModelIndex &index, int role ) const;
                                    QVariant headerData( int section, Qt::Orientation orientation, int role ) const;
                                    void update();
                                
                                
                                private:
                                    static PLAYERHOLDER* m_playerHolder;
                                };@
                                

                                But that didnt work :(

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

                                  How about:

                                  @
                                  #include <QtCore>
                                  #include <player.h>
                                  #include <datasource.h>
                                  #include <contactmodel.h>
                                  class QAbstractItemModel;

                                  class PLAYERHOLDER
                                  {
                                  private:
                                  class CONTACTMODEL; //forward declaration

                                  public:
                                  static PLAYERHOLDER* getInstance();
                                  void createPlayer(PLAYER player);
                                  void updatePlayer(int id);
                                  void deletePlayer(int id);
                                  PLAYER
                                  findPlayer(int id);
                                  void loadPlayers(int teamid);

                                  QAbstractItemModel* model() ;
                                  

                                  private:
                                  PLAYERHOLDER();
                                  static PLAYERHOLDER thePlayerholder;
                                  QHash<int, PLAYER
                                  > playerlist;
                                  DATASOURCE *datasource;
                                  mutable CONTACTMODEL *m_model;
                                  };
                                  @

                                  @
                                  //in PLAYERHOLDER.cpp

                                  #include <QAbstractTableModel>

                                  class PLAYERHOLDER::CONTACTMODEL : public QAbstractTableModel
                                  {
                                  public:
                                  explicit CONTACTMODEL(PLAYERHOLDER* holder)
                                  : QAbstractItemModel()
                                  { //implement here }

                                      int rowCount( const QModelIndex &parent ) const 
                                      { 
                                          // implement here
                                      }
                                  
                                      // same for other methods
                                  
                                  private:
                                      PLAYERHOLDER* m_playerHolder; // NO need to make it static
                                  };
                                  

                                  //implement PLAYERHOLDER here under

                                  @

                                  1 Reply Last reply
                                  0
                                  • D Offline
                                    D Offline
                                    DaFranzl
                                    wrote on last edited by
                                    #27

                                    Ok, so I forward declared the class, but you said i have to define the signal datachanged, the problem i have is that nested classes aren't allowed to have signal and slots. So how can i implement it? Also if I try to add Q_Object macro into CONTACTMODEL, I get compile errors about unresolved extern symbol metaObject.

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

                                      No, I did not say that. QAbstractItemModel already defines it, you just need to make sure the signal is emitted when needed. So, you don't need moc or Q_OBJECT in the class.

                                      1 Reply Last reply
                                      0
                                      • D Offline
                                        D Offline
                                        DaFranzl
                                        wrote on last edited by
                                        #29

                                        Ok, I tryed to implement the functions insertRows() and removeRows(). I have an example from my book:

                                        @bool TableModel::insertRows(int row, int count, const QModelIndex&)
                                        {
                                        beginInsertRows(QModelIndex(), row, row + count - 1);
                                        for (int i = 0; i < count; ++i)
                                        zipcodes.insert(row, ZipcodeItem());
                                        endInsertRows();
                                        return true;
                                        }@

                                        But i dont think this one would work, cause it inserts items into the store. But in my case it is like, the function createPlayer() is called and then the model needs to update. Also the update function, it is called then it needs to find out where the player is stored and then edits it. But I have no idea after reading this book how to implement this with my api.

                                        1 Reply Last reply
                                        0
                                        • I Offline
                                          I Offline
                                          issam
                                          wrote on last edited by
                                          #30

                                          the best choice is QStandardItemModel (or QAbstractTableModel) + QTableView :

                                          the model : to store and control data
                                          the view : to display the data

                                          you can also download this example : http://www.iissam.com/sources/codes.php (AHP method)

                                          http://www.iissam.com/

                                          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