[SOLVED] manipulating qmainwindow widget through qdialog
-
i found this : http://www.qtcentre.org/threads/24755-How-to-add-data-into-the-QTableWidget
but i get the following error : src/dialog.cpp:5: error: class 'Dialog' does not have any field named 'm_table' -
solved it on my own :
mainwindow.cpp
@
MainWindow::openDialog()
{
Dialog d(this, tableWidget);
d.exec();
}
@
dialog.cpp
@
QTableWidget *p_table;
Dialog::Dialog(QWidget * parent , QTableWidget * table) : QDialog(parent)
{
setupUi(this);
p_table = table;
}
@ -
Hi,
and that's exaclty how to get the pointer to the parent:
@
QTableWidget *p_table;
Dialog::Dialog(QWidget * parent , QTableWidget * table) : QDialog(parent)
{
setupUi(this);
p_table = table;
}
@The parent pointer. That could be cast to the MainWindow class. Or the main window could make a connect to a custom signal:
@
MainWindow::openDialog()
{
Dialog d(this, tableWidget);
connect(&d, SIGNAL(MyCustomSignal(...)), this, SLOT(MySlotToAddSomeStuff(...)));
d.exec();
}@ -
Using signals and slots would be the better way, IMHO. And mor Qt'ish, too.
This also has the advantage, that your dialog needs not to know anything about your list widget. In case you change your mind and replace the list widget by another widget, you only have to change the main windows implementation, but not the dialogs, as the signal remains the same.
Basically the code is like this:
dialog.h
@
class Dialog: public QDialog {
public:
Dialog(QWidget *parent);
// ... more stuffprotected slots:
void saveButtonClicked();signals:
void requestNewEntry(const QString &name, int amount, QDate date);
}
@dialog.cpp
@
Dialog::Dialog(QWidget *parent) : QDialog(parent)
{
// do your usual stuff here
connect(saveButton, SIGNAL(clicked()), this, SLOT(saveButtonClicked()));
}void Dialog::saveButtonClicked()
{
QString name = lineEdit->text();
int amount = intSpinBoxl->value();
QDate date = dateEdit->date();
emit requestNewEntry(name, amount, date);
}
@mainwindow.h
@
class MainWindow: public QMainWindow {
public:
// constructors etc.public slots:
void addEntry(const QString &name, int amount, QDate date);protected slots:
void showDialog();
}
@mainwindow.cpp
@
void MainWindow::openDialog()
{
Dialog *d = new Dialog(this);
connect(d, SIGNAL(requestNewEntry(QString, int, QDate)), this, SLOT(addEntry(QString, int, QDate)));
d->show()
}void MainWindow::addEntry(const QString &name, int amount, QDate date)
{
// create the items here depending on the value
QListItem *i1 = new QListItem(val1);
table->setItem(row, col, i1)
}
@ -
[quote author="peppe" date="1292491829"]I like the fact that I write how the solution should be, and someone else writes the corresponding code :-D[/quote]
Shouldn't that be always the case in a our ideal world? ;-)
-
You might be lucky, I'm a rent-that-coding-guy :-)