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. [SOLVED] Communication between classes

[SOLVED] Communication between classes

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.2k 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.
  • I Offline
    I Offline
    Imhotep
    wrote on last edited by
    #1

    My code is set up in this way: the main window with a QTableWidget, and a control panel created with a different class from the MainWindow. In the control panel control there is a QListWidget where I want to load the titles of the header from the table, but being QTableWidget private, how can I pass data between the two classes?

    mainwindow.h
    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include "QControlPanel.h"
    #include <QMainWindow>
    #include <QWidget>
    #include <QtGui>
    #include <QTableWidget>

    namespace Ui {
    class MainWindow;
    class GenerateXML;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);

    ~MainWindow();
    

    public slots:

    private:
    Ui::MainWindow ui;
    QTableWidget
    m_pTableWidget;
    QControlPanel* preferences;
    };

    #endif // MAINWINDOW_H
    @

    mainwindow.cpp
    @
    #include "mainwindow.h"

    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    {
    this->setWindowIcon(QIcon("DrawItem.ico"));
    this->setWindowTitle("DrawItem");
    resize(890, 475);

    m_pTableWidget = new QTableWidget(this);
    m_TableHeader<<"A"<<"B"<<"C"<<"D"<<"E";
    m_pTableWidget->setHorizontalHeaderLabels(m_TableHeader);
    m_pTableWidget->resizeColumnsToContents();
    m_pTableWidget->verticalHeader()->setVisible(false);
    m_pTableWidget->move(30, 75);
    m_pTableWidget->resize(410, 151);
    
    preferences = new QControlPanel(this);
    

    }
    @

    QControlPanel.h
    @
    #include <QDialog>
    #include <QTableWidget>

    #ifndef QCONTROLPANEL_H
    #define QCONTROLPANEL_H

    class QControlPanel : public QDialog
    {
    Q_OBJECT

    public:
    explicit QControlPanel(QWidget *parent = 0);

    ~QControlPanel();
    

    public slots:
    void customHeader(QTableWidget *table);

    private:
    QListWidget *headerlist;

    private slots:

    };

    #endif
    @

    QControlPanel.cpp
    @
    #include "QControlPanel.h"

    QControlPanel::QControlPanel(QWidget *parent) :
    QDialog(parent)
    {
    headerlist = new QListWidget(inputHeader);
    headerlist->setGeometry(140, 15, 140, 130);
    }
    @

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

      Hi,

      You can add a QStringList getter and a setter for that. Use the getter to update the control panel data before showing it and then the setter once your'e done.

      Doing so the ControlPanel doesn't need to know what he'll be modifying.

      On a side note, you should avoid creating Q prefixed classes, you might create confusion with Qt's own classes and also end up clashing with something already use.

      Hope it helps

      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
      0
      • I Offline
        I Offline
        Imhotep
        wrote on last edited by
        #3

        Yes, it help me :) thanks!

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

          Then if it's all working now, please update the thread title prepending [solved] so other forum users may know a solution has been found :)

          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
          0

          • Login

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