[SOLVED] Communication between classes
-
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_OBJECTpublic:
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_Hclass QControlPanel : public QDialog
{
Q_OBJECTpublic:
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);
}
@ -
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
-
Yes, it help me :) thanks!
-
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 :)