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. QTableView & CheckBox delegate alignmnent
QtWS25 Last Chance

QTableView & CheckBox delegate alignmnent

Scheduled Pinned Locked Moved General and Desktop
43 Posts 4 Posters 37.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.
  • G Offline
    G Offline
    giesbert
    wrote on last edited by
    #21

    [quote author="hsfougaris" date="1301549859"]Well, how can you easily use it when using any QSql** models (which is a very common case one would think)? You have to subclass every time...
    [/quote]

    Where should the relational model know from if the cell should be checkable. Depending on the returned value (the tyüpe of the QVariant), the editor widget is choosen. What is a default editor for, lets say a boolean, depends whome you ask. It could be a check box, or a combo with true/false.

    Nokia Certified Qt Specialist.
    Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

    1 Reply Last reply
    0
    • H Offline
      H Offline
      hsfougaris
      wrote on last edited by
      #22

      That's why the QTableView class should be much richer in my opinion (like almost every other grid developed). It could have properties like setColumnWidget(...) ...

      The fact of the matter is that trying to use QSql*** with a QTableView with anything other than text is a pain, and there is no straightforward way to handle very common needs and scenarios.
      You can't even center align a column without a delegate!

      If you can't say what you mean, you'll never be able to mean what you say.

      1 Reply Last reply
      0
      • H Offline
        H Offline
        hsfougaris
        wrote on last edited by
        #23

        [quote author="hsfougaris" date="1301550103"]Back to the main issue, I found this post http://lists.qt.nokia.com/pipermail/qt-interest/2009-January/001833.html which is pretty much about the same thing.

        It seems to be a bug in QSqlRelationalDelegate , and a workaround for the createEditor is discussed.
        But I can't seem to find the code.
        In src\sql\models\qsqlrelationaldelegate.cpp there is only the following
        @
        /*!
        \fn QWidget *QSqlRelationalDelegate::createEditor(QWidget *parent,
        const QStyleOptionViewItem &option,
        const QModelIndex &index) const
        \reimp
        */
        @
        Does anyone know where the actual implementation is?
        [/quote]

        It seems the code is in the header... :)

        If you can't say what you mean, you'll never be able to mean what you say.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #24

          [quote author="hsfougaris" date="1301551606"]That's why the QTableView class should be much richer in my opinion (like almost every other grid developed). It could have properties like setColumnWidget(...) ...

          The fact of the matter is that trying to use QSql*** with a QTableView with anything other than text is a pain, and there is no straightforward way to handle very common needs and scenarios.
          You can't even center align a column without a delegate![/quote]

          If you need a richer table view, look at some of the commercial solutions that exist. I also found some things I would like to have in a TableView, but I'm sure, not 90% of the user would like to have it, perhaps on 10 %. And I think, it's the same with the SQL stuff. In our company, we use many tables, but not a single QSqlXXX class. We have custom data providers.

          And I think, a QSFPM as "Man in the middle" to achieve this, is not the worst solution.

          And via the delegates, you achieve exaclty the same as with setColumnWidget. Create a delegate and use setColumnDelegate. Model - View - Delegate is a very good pattern indeed. It's a derivate of the Model-View-Controler which is very common.

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          1 Reply Last reply
          0
          • H Offline
            H Offline
            hsfougaris
            wrote on last edited by
            #25

            Is there anything other that one ICS makes? (because that is priced insanely...)
            I had a look at one from DevMachines, which looks promising but is not ready for prime time yet.

            I haven't been able to find other ones, so I would appreciate any directions.

            If you can't say what you mean, you'll never be able to mean what you say.

            1 Reply Last reply
            0
            • H Offline
              H Offline
              hsfougaris
              wrote on last edited by
              #26

              Also if QSFPM means QSortFilterProxyModel, I agree it's not bad as a solution (especially after what I managed to build with a little help); I just wish I could do a few more things with it...

              If you can't say what you mean, you'll never be able to mean what you say.

              1 Reply Last reply
              0
              • H Offline
                H Offline
                hsfougaris
                wrote on last edited by
                #27

                Ok, I created my own subclass of QSqlRelationalDelegate and now everyhting works.

                Here is the related code:
                @

                QWidget *mySqlRelationalDelegate::createEditor(QWidget *aParent, const QStyleOptionViewItem &option, const QModelIndex &index) const {

                const QSqlRelationalTableModel *sqlModel = qobject_cast<const QSqlRelationalTableModel *>(index.model());
                QSqlTableModel *childModel = sqlModel ? sqlModel->relationModel(index.column()) : 0;
                
                if (!childModel )
                {
                    const QSortFilterProxyModel* proxyModel = qobject_cast<const QSortFilterProxyModel *>(index.model());
                    if (proxyModel)
                    {
                        sqlModel = qobject_cast<const QSqlRelationalTableModel *>(proxyModel->sourceModel());
                        childModel = sqlModel ? sqlModel->relationModel(index.column()) : 0;
                    }
                }
                
                if (!childModel)
                {
                    return QItemDelegate::createEditor(aParent, option, index);
                }
                
                QComboBox *combo = new QComboBox(aParent);
                combo->setModel(childModel);
                combo->setModelColumn(childModel->fieldIndex(sqlModel->relation(index.column()).displayColumn()));
                combo->installEventFilter(const_cast<mySqlRelationalDelegate *>(this));
                
                return combo;
                

                }

                void mySqlRelationalDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
                {
                QString strVal = "";
                const QSqlRelationalTableModel *sqlModel = qobject_cast<const QSqlRelationalTableModel >(index.model());
                if (!sqlModel )
                {
                const QSortFilterProxyModel
                proxyModel = qobject_cast<const QSortFilterProxyModel *>(index.model());
                if (proxyModel) {
                strVal = proxyModel->data(index).toString();
                }
                } else {
                strVal = sqlModel->data(index).toString();
                }

                QComboBox *combo = qobject_cast<QComboBox *>(editor);
                if (strVal.isEmpty() || !combo) {
                    QItemDelegate::setEditorData(editor, index);
                    return;
                }
                
                combo->setCurrentIndex(combo->findText(strVal));
                

                }

                void mySqlRelationalDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
                {
                if (!index.isValid())
                return;

                QSqlRelationalTableModel *sqlModel = qobject_cast<QSqlRelationalTableModel *>(model);
                QSortFilterProxyModel* proxyModel = NULL;
                if (!sqlModel )
                {
                    proxyModel = qobject_cast<QSortFilterProxyModel *>(model);
                    if (proxyModel)
                         sqlModel = qobject_cast<QSqlRelationalTableModel *>(proxyModel->sourceModel());
                }
                
                QSqlTableModel *childModel = sqlModel ? sqlModel->relationModel(index.column()) : 0;
                QComboBox *combo = qobject_cast<QComboBox *>(editor);
                if (!sqlModel || !childModel || !combo) {
                    QItemDelegate::setModelData(editor, model, index);
                    return;
                }
                
                int currentItem = combo->currentIndex();
                int childColIndex = childModel->fieldIndex(sqlModel->relation(index.column()).displayColumn());
                int childEditIndex = childModel->fieldIndex(sqlModel->relation(index.column()).indexColumn());
                
                
                if (proxyModel) {
                    proxyModel->setData(index, childModel->data(childModel->index(currentItem, childColIndex), Qt::DisplayRole), Qt::DisplayRole);
                    proxyModel->setData(index, childModel->data(childModel->index(currentItem, childEditIndex), Qt::EditRole), Qt::EditRole);
                } else {
                    sqlModel->setData(index, childModel->data(childModel->index(currentItem, childColIndex), Qt::DisplayRole), Qt::DisplayRole);
                    sqlModel->setData(index, childModel->data(childModel->index(currentItem, childEditIndex), Qt::EditRole), Qt::EditRole);
                }
                

                }
                @

                I'll try making it a Wiki!

                If you can't say what you mean, you'll never be able to mean what you say.

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

                  Thanks for posting back the code!

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    giesbert
                    wrote on last edited by
                    #29

                    [quote author="hsfougaris" date="1301553694"]Also if QSFPM means QSortFilterProxyModel, I agree it's not bad as a solution (especially after what I managed to build with a little help); I just wish I could do a few more things with it...[/quote]

                    Yes, QSFPM is a (here) common abbreveation for QSortFilterProxyModel :-)

                    Perhaps we should add a wiki page with common abbreveation sused here :-)

                    Nokia Certified Qt Specialist.
                    Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

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

                      <dreaming>
                      We should have a feature where such abbreviations are automatically underlined with a dotted line (perhaps only for the first use in a post), which show an explanation on the meaning of that abbreviation (perhaps automagically retreived from that wiki page you talk about).
                      </dreaming>

                      1 Reply Last reply
                      0
                      • H Offline
                        H Offline
                        hsfougaris
                        wrote on last edited by
                        #31

                        I tried to post the source as a wiki ( I created 2 separate pages - one for the delegate and one for the sortproxy thing), but I'm sure I messed it up.
                        Am I supposed to add them to a category, or does an admin do that?

                        I tried adding the [[Category... thing but it appeared as plain text.

                        If you can't say what you mean, you'll never be able to mean what you say.

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

                          The category thing should work, but you do need to remove spaces between the brackets. Problem is: that is hard to show in the wiki itself, because the text will then be interpreted as formatting...

                          If you want: post a link, and I'll have a look.

                          1 Reply Last reply
                          0
                          • H Offline
                            H Offline
                            hsfougaris
                            wrote on last edited by
                            #33

                            http://developer.qt.nokia.com/wiki/QSortFilterProxyModel_subclass_for_readonly_columns_columns_with_checkboxes_and_password_columns

                            http://developer.qt.nokia.com/wiki/QSqlRelationalDelegate_subclass_that_works_with_QSqlRelationalTableModel

                            If you can't say what you mean, you'll never be able to mean what you say.

                            1 Reply Last reply
                            0
                            • G Offline
                              G Offline
                              giesbert
                              wrote on last edited by
                              #34

                              author="Andre" date="1301563925"]<dreaming>
                              We should have a feature where such abbreviations are automatically underlined with a dotted line (perhaps only for the first use in a post), which show an explanation on the meaning of that abbreviation (perhaps automagically retreived from that wiki page you talk about).
                              </dreaming>
                              [/quote]

                              Make a feature request from this in Jira :-) The idea is cool.

                              [quote author="hsfougaris" date="1301564361"]I tried to post the source as a wiki ( I created 2 separate pages - one for the delegate and one for the sortproxy thing), but I'm sure I messed it up.
                              Am I supposed to add them to a category, or does an admin do that?

                              I tried adding the [[Category... thing but it appeared as plain text.[/quote]

                              I added the category for you. Just have a look with edit :-)

                              Nokia Certified Qt Specialist.
                              Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                              1 Reply Last reply
                              0
                              • H Offline
                                H Offline
                                hsfougaris
                                wrote on last edited by
                                #35

                                Thanks... Do you have any references for suppliers of tableView alternatives?

                                If you can't say what you mean, you'll never be able to mean what you say.

                                1 Reply Last reply
                                0
                                • G Offline
                                  G Offline
                                  giesbert
                                  wrote on last edited by
                                  #36

                                  Hi,

                                  not more than you already found: devmachines and ICS. I'm sure, there are more, but I don't know who. You can search "Qt apps":http://www.qt-apps.org/

                                  One side note to your wiki:

                                  I found one error:

                                  the data/setdata and flags methods should be public, as they are public in the base class. Making the protected in your derived class leads to strange behavior:

                                  People using the base class pointers are able to call them, people using the pointers of type of your class may not. Please make them public.

                                  Nokia Certified Qt Specialist.
                                  Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

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

                                    [quote author="hsfougaris" date="1301564749"]http://developer.qt.nokia.com/wiki/QSortFilterProxyModel_subclass_for_readonly_columns_columns_with_checkboxes_and_password_columns

                                    http://developer.qt.nokia.com/wiki/QSqlRelationalDelegate_subclass_that_works_with_QSqlRelationalTableModel

                                    [/quote]

                                    Pages, including categories, look fine to me.

                                    1 Reply Last reply
                                    0
                                    • H Offline
                                      H Offline
                                      hsfougaris
                                      wrote on last edited by
                                      #38

                                      Did you already do it in the wiki? Because I just looked at the wiki, and it's been removed.

                                      If you can't say what you mean, you'll never be able to mean what you say.

                                      1 Reply Last reply
                                      0
                                      • H Offline
                                        H Offline
                                        hsfougaris
                                        wrote on last edited by
                                        #39

                                        [quote author="Andre" date="1301565818"]
                                        [quote author="hsfougaris" date="1301564749"]http://developer.qt.nokia.com/wiki/QSortFilterProxyModel_subclass_for_readonly_columns_columns_with_checkboxes_and_password_columns

                                        http://developer.qt.nokia.com/wiki/QSqlRelationalDelegate_subclass_that_works_with_QSqlRelationalTableModel

                                        [/quote]

                                        Pages, including categories, look fine to me.

                                        [/quote]

                                        Yes, Gerolf was nice enough to fix them for me.

                                        If you can't say what you mean, you'll never be able to mean what you say.

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

                                          [quote author="hsfougaris" date="1301565899"]

                                          Yes, Gerolf was nice enough to fix them for me.[/quote]

                                          Damn, that guy is fast ;-)

                                          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