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. error: 'QWidget::QWidget(const QWidget&)' is private Q_DISABLE_COPY(QWidget)
Forum Updated to NodeBB v4.3 + New Features

error: 'QWidget::QWidget(const QWidget&)' is private Q_DISABLE_COPY(QWidget)

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 3.8k 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.
  • NiagarerN Offline
    NiagarerN Offline
    Niagarer
    wrote on last edited by Niagarer
    #1

    Hey,
    Two errors here:

    1: ...\qwidget.h: error: 'QWidget::QWidget(const QWidget&)' is private
         Q_DISABLE_COPY(QWidget) // I guess this is probably the main problem
                        ^
    2: ...\qlist.h: error: use of deleted function 'QWidget::QWidget(const QWidget&)'
    

    These I get multiple times at different locations in the two files.
    I am trying to set up a easy QAbstractListModel with widgets (just QWidgets with different background color).
    This is where I try to add widgets to the model in mainwindow.cpp :

        QList<QWidget> widgets;
        QWidget w1;
        w1.setStyleSheet("background-color: black");
        QWidget w2;
        w1.setStyleSheet("background-color: blue");
        QWidget w3;
        w1.setStyleSheet("background-color: green");
        widgets << w1 << w2 << w3;
        model->addPieces(widgets); // model is a object of a custom QAbstractListModel derived class
    

    addPieces in the model class:

    void MyListModel::addPieces(const QList<QWidget> &widgts)
    

    I am guided by the puzzle-example.
    Can you tell me, why I get these errors? What am I missing?
    Thanks for answers!

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      QObjects can't be copied. you can have a QList<QWidget*> but not a QList<QWidget>.

      Having said that. Stop immediately whatever you are doing.
      The model should not take care of rendering.
      If you want to color a cell just use QStandardItemModel and call model->setData(model->index(row,column),QBrush(Qt::black),Qt::BackgroundRole );
      See http://doc.qt.io/qt-5/model-view-programming.html for more details on model/view/delegate

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      NiagarerN 1 Reply Last reply
      2
      • VRoninV VRonin

        QObjects can't be copied. you can have a QList<QWidget*> but not a QList<QWidget>.

        Having said that. Stop immediately whatever you are doing.
        The model should not take care of rendering.
        If you want to color a cell just use QStandardItemModel and call model->setData(model->index(row,column),QBrush(Qt::black),Qt::BackgroundRole );
        See http://doc.qt.io/qt-5/model-view-programming.html for more details on model/view/delegate

        NiagarerN Offline
        NiagarerN Offline
        Niagarer
        wrote on last edited by Niagarer
        #3

        @VRonin
        Ok, thanks.
        Just thought, this would be a legitim way, because I just looked to the puzzle-example and there it is also made this way (but with Pixmaps) and this example shows what I want to have in the end.

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

          Hi,

          QPixmap is not a QObject that's why it works in that case. Not all Qt classes derive from QObjects.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          NiagarerN 1 Reply Last reply
          2
          • SGaistS SGaist

            Hi,

            QPixmap is not a QObject that's why it works in that case. Not all Qt classes derive from QObjects.

            NiagarerN Offline
            NiagarerN Offline
            Niagarer
            wrote on last edited by Niagarer
            #5

            @SGaist
            Ohhh, that makes sense.
            Now I use a delegate class and I want to display a custom widget by me to get what I tried to do above. The model holds only the title (and some more icons later) and the view just should display these custom widgets as items (and in the custom widgets, the corresponding title will be shown). How can I make my delegate class creating and displaying these widgets (I already have the widget class) and using them as items in the ListView?

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6
              • Do you really need to display widgets?
              • could you handle the paint efficiently yourself?

              If you really answered yes and no then you can use: https://pastebin.com/XrppLZ3m but be mindful performance will be impacted

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              NiagarerN 1 Reply Last reply
              0
              • VRoninV VRonin
                • Do you really need to display widgets?
                • could you handle the paint efficiently yourself?

                If you really answered yes and no then you can use: https://pastebin.com/XrppLZ3m but be mindful performance will be impacted

                NiagarerN Offline
                NiagarerN Offline
                Niagarer
                wrote on last edited by
                #7

                @VRonin
                Ok... I just don't get the problem here...
                Everywhere in every program we use, we have lists of widgets (or a treeView), that are draggabe into another area/over another widget, that then reacts to it. Why is this such a problem?
                I don't know exactely what you mean with handeling the paint event myself. Why can't I just change the displaying part. Anywhere, there must be a function, that draws the items of the list. Why can't I just redict the painting signal to the correseponding widget?

                mrjjM 1 Reply Last reply
                0
                • NiagarerN Niagarer

                  @VRonin
                  Ok... I just don't get the problem here...
                  Everywhere in every program we use, we have lists of widgets (or a treeView), that are draggabe into another area/over another widget, that then reacts to it. Why is this such a problem?
                  I don't know exactely what you mean with handeling the paint event myself. Why can't I just change the displaying part. Anywhere, there must be a function, that draws the items of the list. Why can't I just redict the painting signal to the correseponding widget?

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #8

                  @Niagarer
                  Having a list of real widgets is heavy to draw.
                  if you plan on run on smaller boards ,it will not
                  scroll nicely if u have many items.
                  The solution to this - is a delegate the at draw the look of the widget,
                  but it IS not a full widget unless you click on it.
                  When clicked Then delegate via Editor, use a the real widget and user can interact with it on the
                  current select row/col. As soon as its not the active one, its only drawn by
                  manual painter operations.
                  That is how a delegate help performance.
                  By NOt being a big is list of real widgets but only the current one is real and rest
                  is not.

                  Try the code VRonin links to. its his creation and very useful!

                  NiagarerN 1 Reply Last reply
                  1
                  • mrjjM mrjj

                    @Niagarer
                    Having a list of real widgets is heavy to draw.
                    if you plan on run on smaller boards ,it will not
                    scroll nicely if u have many items.
                    The solution to this - is a delegate the at draw the look of the widget,
                    but it IS not a full widget unless you click on it.
                    When clicked Then delegate via Editor, use a the real widget and user can interact with it on the
                    current select row/col. As soon as its not the active one, its only drawn by
                    manual painter operations.
                    That is how a delegate help performance.
                    By NOt being a big is list of real widgets but only the current one is real and rest
                    is not.

                    Try the code VRonin links to. its his creation and very useful!

                    NiagarerN Offline
                    NiagarerN Offline
                    Niagarer
                    wrote on last edited by Niagarer
                    #9

                    @mrjj
                    Ok, makes sense.
                    Is it possible to anyway seperate the delegate from the actual widget painting in code? So that I can do something like

                    void MyItemDelegate::paint(QPainter *painter, /* and all other stuff */) const{
                        widgetItems.value(index.row())->myCustomPaintFunction(painter); // widgetItems = QList<WidgetItem*> widgetItems;
                                                        //WidgetItem = just a standalone C++ class
                    }
                    
                    WidgetItem::myCustomPaintFunction(QPainter *painter){
                        // draw the shape
                    }
                    

                    It would be a bit clearer to seperate it this way

                    1 Reply Last reply
                    0
                    • VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #10

                      In general yes, what you wrote, however is conceptually wrong. widgetItems.value(index.row()) implies the delegate owns/is the model which is unusual at least

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      NiagarerN 1 Reply Last reply
                      1
                      • VRoninV VRonin

                        In general yes, what you wrote, however is conceptually wrong. widgetItems.value(index.row()) implies the delegate owns/is the model which is unusual at least

                        NiagarerN Offline
                        NiagarerN Offline
                        Niagarer
                        wrote on last edited by Niagarer
                        #11

                        @VRonin
                        Well yes, I should store the actual data in a model, that is able to return this data correctly.

                        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