[SOLVED] manipulating qmainwindow widget through qdialog



  • hi, i have a qtablewidget in my mainwindow and when a dialog opens, there is a bunch of qlinedit and when i click the button add in the qdialog, the data in the qlinedits must be inserted in the qtablewidget in the mainwindow without closing the qdialog coz i have to insert another data in the qtablewidget but the problem is that the data is not reflected in the qtablewidget.

    mainwindow has a public slot for the qdialog to call. it run bcoz i tried to put a qmessagebox in each line but the qtablewidget setItem is not working.

    help what is wrong with it?

    this is my main window contructor :
    @
    MainWindow::MainWindow( QWidget * parent, Qt::WFlags f) : QMainWindow(parent, f)
    { }
    @
    this is my dialog child contructor :
    @
    DialogChild::DialogChild( QWidget * child ) : QDialog(child)
    { }
    @



  • Could you give some more code, because of this, I don't see the problem. More example of the code could help to find the problem.



  • First of all, write your questions in proper English.

    Second, I don't see anything wrong in your design. You said you have a slot in your MainWindow, with a signature like
    @
    appendRow(const QList<Item> &);
    @

    Therefore, all you need is

    declare a corresponding signal (like, rowAdded()) in the Dialog

    connect the Dialog signal to the MainWindow slot

    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.



  • this is my code to add items in qtablewidget.

    @
    void MainWindow::setData(QString text)
    int i = tableWidget->rowCount();
    tablewidget->insertRow(i);
    QTableWidgetItem *txt = new QTableWidgetItem(text);
    tableWidget->setItem(i, 0, txt);
    @

    mainwindow.h
    @
    public slots:
    void setData(QString);
    private slots:
    void openDialog();
    @

    dialog.cpp
    @
    void Dialog::clickAdd()
    {
    MainWindow form;
    form.setData(lineEdit->text());
    lineEdit->setData(""); // Empty the lineEdit coz I need to insert another data
    }
    @



  • You instantiate a MainWindow in clickAdd(), add some data, and when the method ends, the form goes out of scope and is destroyed. Not a Qt problem, but some misunderstanding of C/C++ scopes for variables and objects.

    peppe already suggested a proper way to achieve this task.



  • You need a pointer to the main window in your dialog. Perhaps it's the parent? And you should connect the signal of the dialog with the slot of the main window.



  • 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 stuff

    protected 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.