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. Some questions regarding the star delegate example.
Forum Updated to NodeBB v4.3 + New Features

Some questions regarding the star delegate example.

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 250 Views 2 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
    andi456
    wrote on last edited by
    #1

    Hi,

    as I'd like to accomplish something similar but with some simple lines instead of diamonds/stars, I studied the star delegate example, but still have some questions.

    Is it correct that Q_DECLARE_METATYPE(StarRating) enables StarRating class instances to be displayed in a table cell, i.e., using the model/view approach it would be used by the data method?

    The constructor of the StarRating class confuses me a little:

    StarRating::StarRating(int starCount, int maxStarCount)
        : myStarCount(starCount),
          myMaxStarCount(maxStarCount) {....}
    
    

    Do myStarCount and myMaxStarCount - so to say - receive the values of starCount and maxStarCount or how should I read the syntax?

    Last but not least, at the end of the description there's the hint that the same would be achievable with QAbstractItemDelegate::editorEvent(), i.e., without having to write a separate subclass of QWidget. Would this more complicated or easier than creating a subclass?

    Kind regards,

    Andreas

    PS: I am referring to the current documentation: Star delegate example

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

      Hi,

      1. No, Q_DECLARE_METATYPE has nothing to do with displaying something in a cell. It's part of the mechanism that allows to use custom types in for example signals and slots.

      2. Yes, the values are passed to the member variables in the initialisation list.

      3. From the top of my head, it shouldn't be that much more complicated.

      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
      1
      • A andi456

        Hi,

        as I'd like to accomplish something similar but with some simple lines instead of diamonds/stars, I studied the star delegate example, but still have some questions.

        Is it correct that Q_DECLARE_METATYPE(StarRating) enables StarRating class instances to be displayed in a table cell, i.e., using the model/view approach it would be used by the data method?

        The constructor of the StarRating class confuses me a little:

        StarRating::StarRating(int starCount, int maxStarCount)
            : myStarCount(starCount),
              myMaxStarCount(maxStarCount) {....}
        
        

        Do myStarCount and myMaxStarCount - so to say - receive the values of starCount and maxStarCount or how should I read the syntax?

        Last but not least, at the end of the description there's the hint that the same would be achievable with QAbstractItemDelegate::editorEvent(), i.e., without having to write a separate subclass of QWidget. Would this more complicated or easier than creating a subclass?

        Kind regards,

        Andreas

        PS: I am referring to the current documentation: Star delegate example

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by
        #3

        @andi456

        To extend @SGaist 's answers a lil bit:

        Is it correct that Q_DECLARE_METATYPE(StarRating) enables StarRating class instances to be displayed in a table cell, i.e., using the model/view approach it would be used by the data method?

        This is not the purpose of Q_DECLARE_METATYPE but in this case it comes in handy to use it like this. It's even noted in the example documentation itself:

        The Q_DECLARE_METATYPE() macro makes the type StarRating known to QVariant, making it possible to store StarRating values in QVariant.

        (taken from here)

        So it's possible to do this:

                QTableWidgetItem *item3 = new QTableWidgetItem;
                item3->setData(0,
                       QVariant::fromValue(StarRating(staticData[row].rating)));
        

        Notice the call to QVariant::fromValue to convert a StarRating to a QVariant.

        because QTableWidgetItem::setData is defined as:

        void QTableWidgetItem::setData(int role, const QVariant &value)
        

        Do myStarCount and myMaxStarCount - so to say - receive the values of starCount and maxStarCount or how should I read the syntax?

        This is basic C++. It's the fast and short form of

        StarRating::StarRating(int starCount, int maxStarCount)
        {
            myStarCount = starCount;
            myMaxStarCount = maxStarCount;
        }
        

        Would this more complicated or easier than creating a subclass?

        It's less code and on the same level of complexity, I would say.
        Basically everything you do in your StarEditor goes in the editorEvent function.
        There you tell your Item View in which way you want to edit the data of type StarRating (treated as QVariant) represented by StarDelegate.


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        1 Reply Last reply
        1
        • A Offline
          A Offline
          andi456
          wrote on last edited by
          #4

          Thank you for your answers.

          1 Reply Last reply
          0
          • A andi456 has marked this topic as solved on

          • Login

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