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. Custom QWidget inside a QTableWidgetItem

Custom QWidget inside a QTableWidgetItem

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 866 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.
  • R Offline
    R Offline
    riczam
    wrote on last edited by riczam
    #1

    Hello everybody,
    I'm trying to put a custom Widget called PushButtonElimina inside a cell in QTableWidget but PushButtonElimina is showed inside new window.

    In the snippet below the code that I used to create custom widget

    PushButtonElimina::PushButtonElimina(QWidget *parent, int riga) :
      QWidget(parent), riga{riga}
    {
      pWidget = new QWidget();
      azione = new QPushButton();
      azione->setParent(parent);
      pixmap = new QPixmap(":/img/Resources/images/notification-error_114458.png");
      QIcon icona(*pixmap);
      azione->setIconSize((QSize(32, 32)));
      azione->setIcon(icona);
      azione->setFixedSize(32, 32);
      azione->setFlat(true);
      pLayout = new QHBoxLayout(pWidget);
      pLayout->addWidget(azione);
      pLayout->setAlignment(Qt::AlignCenter);
      pLayout->setContentsMargins(50, 0, 50, 0);
      pWidget->setLayout(pLayout);
    
      connect(azione, SIGNAL(clicked()), this, SLOT(on_pushButtonElimina_clicked()));
    }
    

    and here where I use it

    PushButtonElimina *azione = new PushButtonElimina(ui->tableWidgetIngredienti, riga);
    
      connect(azione, SIGNAL(eliminaRiga()), this, SLOT(on_pushButtonElimina_clicked()));
    
      ui->tableWidgetIngredienti->setCellWidget(
        riga,
        2,
        azione
      );
    

    Any suggestion to solve this problem? I prefer to create a custom widget because I used it around all the project and I want to avoid code replications.

    Thanks in advanced

    Ric

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @riczam said in Custom QWidget inside a QTableWidgetItem:

      pWidget = new QWidget();

      Here you create a new widget and put all other widgets into it for no reason.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      R 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        @riczam said in Custom QWidget inside a QTableWidgetItem:

        pWidget = new QWidget();

        Here you create a new widget and put all other widgets into it for no reason.

        R Offline
        R Offline
        riczam
        wrote on last edited by
        #3

        @Christian-Ehrlicher do you suggest to use only QPushButton and put it inside the cell?
        How can I set padding in the cell if I do not set a layout?

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @riczam said in Custom QWidget inside a QTableWidgetItem:

          do you suggest to use only QPushButton and put it inside the cell?

          No, I just told you that you create a new QWidget inside your custom widget and put all in there instead in your custom widget. And this causes your problem.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          R 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            @riczam said in Custom QWidget inside a QTableWidgetItem:

            do you suggest to use only QPushButton and put it inside the cell?

            No, I just told you that you create a new QWidget inside your custom widget and put all in there instead in your custom widget. And this causes your problem.

            R Offline
            R Offline
            riczam
            wrote on last edited by
            #5

            @Christian-Ehrlicher thanks for your tips! I removed the internal Widget and it works as expected.

            Here the correct code

            PushButtonElimina::PushButtonElimina(QWidget *parent, int riga) :
              QWidget(parent), riga{riga}
            {
              azione = new QPushButton();
              azione->setParent(parent);
              pixmap = new QPixmap(":/img/Resources/images/notification-error_114458.png");
              QIcon icona(*pixmap);
              azione->setIconSize((QSize(32, 32)));
              azione->setIcon(icona);
              azione->setFixedSize(32, 32);
              azione->setFlat(true);
              pLayout = new QHBoxLayout(this);
              pLayout->addWidget(azione);
              pLayout->setAlignment(Qt::AlignCenter);
              pLayout->setContentsMargins(50, 0, 50, 0);
              this->setLayout(pLayout);
            
              connect(azione, SIGNAL(clicked()), this, SLOT(on_pushButtonElimina_clicked()));
            }
            
            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