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. How to clone/duplicate QTableWidget?

How to clone/duplicate QTableWidget?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 1.3k 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.
  • S Offline
    S Offline
    Siiiim1119
    wrote on last edited by
    #1

    Hi,

    I need to clone QTableWidget from one QDialog class to another. My end goal is to keep one window displaying the table and if necessary do some changes in the table through other window and later saving changes and saving the new table back. I have managed to open the table in other window but then it dissapears from the original window.

    Getting the original QTableWidget:

    QTableWidget* ViewResults::GetResultsTable()
    {
        return ui->tableWidget;
    }
    

    Constructor of second class where trying to open the duplicate window:

    ChangeResults::ChangeResults(QWidget *parent, ViewResults *viewResults) :
    QDialog(parent),
    ui(new Ui::ChangeResults)
    {
       ui->setupUI(this); 
       ui->verticalLayout->addWidget(mViewResults->GetResultsTable());
    }
    

    Any help is appreciated, Thanks!

    JonBJ 1 Reply Last reply
    0
    • S Siiiim1119

      Hi,

      I need to clone QTableWidget from one QDialog class to another. My end goal is to keep one window displaying the table and if necessary do some changes in the table through other window and later saving changes and saving the new table back. I have managed to open the table in other window but then it dissapears from the original window.

      Getting the original QTableWidget:

      QTableWidget* ViewResults::GetResultsTable()
      {
          return ui->tableWidget;
      }
      

      Constructor of second class where trying to open the duplicate window:

      ChangeResults::ChangeResults(QWidget *parent, ViewResults *viewResults) :
      QDialog(parent),
      ui(new Ui::ChangeResults)
      {
         ui->setupUI(this); 
         ui->verticalLayout->addWidget(mViewResults->GetResultsTable());
      }
      

      Any help is appreciated, Thanks!

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Siiiim1119
      No QWidget can be "clone/duplicate"d, widgets are not copyable. And a widget can only have one parent, so if you re-parent then, yes, it will disappear from where it was.

      You must recreate the widget from its original definition. If you want to preserve some attributes, like say size, you must copy those across,

      For a QTableWidget it has its model built into it, so you cannot (easily) have another one access the same data/model. If you changed to using a QTableView then you can have multiple instances of those all using the same model data, which sounds like what you want.

      S 1 Reply Last reply
      2
      • JonBJ JonB

        @Siiiim1119
        No QWidget can be "clone/duplicate"d, widgets are not copyable. And a widget can only have one parent, so if you re-parent then, yes, it will disappear from where it was.

        You must recreate the widget from its original definition. If you want to preserve some attributes, like say size, you must copy those across,

        For a QTableWidget it has its model built into it, so you cannot (easily) have another one access the same data/model. If you changed to using a QTableView then you can have multiple instances of those all using the same model data, which sounds like what you want.

        S Offline
        S Offline
        Siiiim1119
        wrote on last edited by
        #3

        @JonB Thanks for the input, I will try QTableView.

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

          Hi,

          How do you intend to do these changes ?

          From the looks of it, you should just share the model between the dialogs.

          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

          • Login

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