[SOLVED] manipulating qmainwindow widget through qdialog
-
sorry for my late reply... my internet has a little problem last night..
bq. create a button in the dialog (you may want to make this the default button) and connect its clicked() signal to another Dialog slot, in which you fetch the data from the lineedits and emit the rowAdded() signal.
i dont understand the another dialog part..
as i understand it, dialog, inserts data to the anotherdialog then after dialog is close, mainwindow gets data from another dialog...and how do i make a pointer to the mainwindow or the parent?
-
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();
}@ -
really thanx...
-
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)
}
@ -
I like the fact that I write how the solution should be, and someone else writes the corresponding code :-D
-
[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? ;-)
-
Can I borrow you for some time? I also need people, writing the correct code for my problems :-)))
-
You might be lucky, I'm a rent-that-coding-guy :-)
-
i like this forum... there's a lot of help... just like the codeigniter forum... :D