-3

The following code should call MainWindow function but it is not calling it. I am using QT IDE.

#include "itemdialog.h"
#include "ui_itemdialog.h"
#include "mainwindow.h"

ItemDialog::ItemDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ItemDialog)
{
    ui->setupUi(this);
    setWindowTitle("Status Dialog");

}

ItemDialog::~ItemDialog()
{
    delete ui;
}

void ItemDialog::on_pushButton_clicked()
{
    MainWindow *obj=new MainWindow;
    obj->okbuttonclicked(ui->lineEdit->text());
}

the okbuttonclicked() function is implemented in MainWindow.This Dialog is not supposed to open a new Window. Its function is just to return the input taken from user to MainWindow function.

What is problem in this code. Please Help!

scorpion
  • 97
  • 2
  • 11

3 Answers3

2

This looks like a horrible attempt at implementing an input dialog. You should emit a signal acknowledging that the user has made an input, and connect that signal to a proper slot in your MainWindow and connect that signal and slot in your MainWindow's constructor. Furthermore, if you are just getting a single string in a modal dialog, you should not reinvent the wheel but use QInputDialog instead.

teukkam
  • 4,267
  • 1
  • 26
  • 35
1

You are creating a new (invisible) main window, and calling okbuttonclicked() on that instead of the one already opened. You need to pass the existing QMainWindow into the dialog box, if it is modal this should be done by using it as the dialogs parent. Otherwise create a new constructor arg to carry it.

cmannett85
  • 21,725
  • 8
  • 76
  • 119
  • i have passed Qmainwindow as parent of Qdialog. Now how can i use parent(Qmainwindow) to call its function. can you please tell me with coding – scorpion Mar 06 '12 at 13:46
  • Koying has shown you in his answer. But teukkam's answer is a more appropriate fix. – cmannett85 Mar 06 '12 at 13:48
0

Dirty, but if your QMainWindow is the parent of your QDialog, you could do:

void ItemDialog::on_pushButton_clicked()
{
    MainWindow *obj=qobject_cast<QMainWindow*>(parent());
    if (obj)
        obj->okbuttonclicked(ui->lineEdit->text());
}
Chris Browet
  • 4,156
  • 20
  • 17