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. Class definition issue

Class definition issue

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.1k 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
    gabor53
    wrote on last edited by
    #1

    Hi,
    I have the following class in mydelegate.h:

    #ifndef MYDELEGATE_H
    #define MYDELEGATE_H
    
    #include <QObject>
    #include <QWidget>
    #include <QStyledItemDelegate>
    #include <QStyleOptionViewItem>
    #include <QLineEdit>
    #include <QDebug>
    
    namespace Ui
    {
    class myDelegate;
    }
    
    class myDelegate : public QStyledItemDelegate
    {
        Q_OBJECT
    
    public:
        explicit myDelegate(QWidget *parent = nullptr);
    

    The mydelegate.cpp:

    #include "mydelegate.h"
    
    
    myDelegate::myDelegate(QWidget *parent)
    {
    }
    

    Whenever I run it I get the following warning:
    C:\Programming\Projects\Folkfriends_1_0\mydelegate.cpp:4: warning: unused parameter 'parent' [-Wunused-parameter]
    myDelegate::myDelegate(QWidget *parent)
    ^
    Everything works.Do I need to worry about this (and other unused parameter) warning?
    Thank you.

    Chris KawaC 1 Reply Last reply
    0
    • G gabor53

      Hi,
      I have the following class in mydelegate.h:

      #ifndef MYDELEGATE_H
      #define MYDELEGATE_H
      
      #include <QObject>
      #include <QWidget>
      #include <QStyledItemDelegate>
      #include <QStyleOptionViewItem>
      #include <QLineEdit>
      #include <QDebug>
      
      namespace Ui
      {
      class myDelegate;
      }
      
      class myDelegate : public QStyledItemDelegate
      {
          Q_OBJECT
      
      public:
          explicit myDelegate(QWidget *parent = nullptr);
      

      The mydelegate.cpp:

      #include "mydelegate.h"
      
      
      myDelegate::myDelegate(QWidget *parent)
      {
      }
      

      Whenever I run it I get the following warning:
      C:\Programming\Projects\Folkfriends_1_0\mydelegate.cpp:4: warning: unused parameter 'parent' [-Wunused-parameter]
      myDelegate::myDelegate(QWidget *parent)
      ^
      Everything works.Do I need to worry about this (and other unused parameter) warning?
      Thank you.

      Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      @gabor53 said:

      Do I need to worry about this (and other unused parameter) warning?

      Yes, because it uncovered a serious bug in your code. In your constructor definition you're not passing the parameter to the base class constructor, so it calls a default base constructor, making your instance not having a parent (and thus creating a memory leak).

      When creating QObject derived classes always pass the parent parameter to the base class constructor:

      myDelegate::myDelegate(QWidget* parent) : QStyledItemDelegate(parent)
      {
      }
      

      Btw. It's unusual that an item delegate would need a parent QWidget. QObject should be enough:

      //.h
      explicit myDelegate(QObject* parent = nullptr);
      
      //.cpp
      myDelegate::myDelegate(QObject* parent) : QStyledItemDelegate(parent)
      
      G 1 Reply Last reply
      2
      • Chris KawaC Chris Kawa

        @gabor53 said:

        Do I need to worry about this (and other unused parameter) warning?

        Yes, because it uncovered a serious bug in your code. In your constructor definition you're not passing the parameter to the base class constructor, so it calls a default base constructor, making your instance not having a parent (and thus creating a memory leak).

        When creating QObject derived classes always pass the parent parameter to the base class constructor:

        myDelegate::myDelegate(QWidget* parent) : QStyledItemDelegate(parent)
        {
        }
        

        Btw. It's unusual that an item delegate would need a parent QWidget. QObject should be enough:

        //.h
        explicit myDelegate(QObject* parent = nullptr);
        
        //.cpp
        myDelegate::myDelegate(QObject* parent) : QStyledItemDelegate(parent)
        
        G Offline
        G Offline
        gabor53
        wrote on last edited by
        #3

        @Chris-Kawa
        Thank you. It worked.

        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