how to call Mainwindow method from a childwindow.cpp



  • i tried placing connect function in both the constructors separately but there is no effect, here is my sample code.

    MainWindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <create_config.h>
    #include <QTableView>
    #include <QStandardItemModel>
    #include "JailHouseCell.h"
    #include "JailHouseCellList.h"
    #include "import.h"

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    void get_cell_data(JailHouseCellList &jlist) ;
    void get_mem_data (JailHouseCell &j) ;
    void get_pci_data (JailHouseCell &j) ;
    void get_irq_data (JailHouseCell &j) ;

    ~MainWindow();
    

    private slots:
    void on_pushButton_Create_Config_clicked();

    void on_pushButton_6_clicked();
    
    void on_pushButton_Mem_Config_Add_clicked();
    
    void on_pushButton_Mem_config_Delete_clicked();
    
    void on_pushButton_IRQ_Chips_Add_clicked();
    
    void on_pushButton_IRQ_Chips_Delete_clicked();
    
    void on_pushButton_PCI_Devices_Add_clicked();
    
    void on_pushButton_PCI_Devices_Delete_clicked();
    
    void on_pushButton_Bootfile_clicked();
    
    void on_lineEdit_HM_size_editingFinished();
    

    // void on_lineEdit_HM_size_returnPressed();

    void on_lineEdit_Size_DC_editingFinished();
    

    // void on_lineEdit_Size_DC_returnPressed();

    void on_pushButton_View_Configuration_clicked();
    
    void on_pushButton_Create_clicked();
    

    // void on_lineEdit_NO_CELLS_editingFinished();

    void on_comboBox_CellList_activated(QString index);
    
    void on_pushButton_Update_clicked();
    
    void on_pushButton_Destroy_clicked();
    
    void on_lineEdit_NO_CELLS_textChanged(const QString &arg1) ;
    
    
    void on_pushButton_GenerateConfigFile_clicked();
    
    void on_lineEdit_HM_size_textChanged(const QString &arg1);
    
    void on_pushButton_import_clicked();
    void slot_load_cell_dropdownlist();
    

    private:
    Create_Config *cc ;
    Ui::MainWindow *ui;
    Import *imp ;
    QTableView *view = new QTableView;
    // QStandardItemModel *model; = new QStandardItemModel(25,5,this); //
    JailHouseCell create_cell () ;
    RootCell create_root_cell() ;
    void add_mem(JailHouseCell &j) ;
    void add_irq(JailHouseCell &j) ;
    void add_pci(JailHouseCell &j) ;
    void load_cell(int index) ;
    void load_cell(std::string) ;
    void load_mem(JailHouseCell &j) ;
    void load_irq(JailHouseCell &j) ;
    void load_pci(JailHouseCell &j) ;
    void Enable_Root_Tabs() ;
    void Disable_Root_Tabs() ;
    void Enable_LS_Buttons() ;
    void Disable_LS_Buttons() ;
    void clear_cpu_config() ;
    void load_cell_dropdownlist () ;
    void load_cell_dropdownlist (std::string) ;
    void add_Hypervisor_Memory(RootCell &r);
    void add_Debug_Console(RootCell &r) ;
    void add_Platform_Info(RootCell &r) ;
    void load_Hypervisor_Memory(RootCell &r) ;
    void load_Debug_Console(RootCell &r) ;
    void load_Platform_Info(RootCell &r) ;

    };

    #endif // MAINWINDOW_H

    MainWindow.cpp (constructor)

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {

    ui->setupUi(this);
    QFont font("Plastique",10);
    

    // font.setStyleHint(QFont::Monospace);
    QApplication::setFont(font);
    ui->tabWidget_Main->setHidden(true);

    ui->tabWidget_CELL_CONFIG->setHidden(true);
    ui->tableWidget_cell_config_2->setColumnWidth(4,250);
    Disable_Root_Tabs();
    Disable_LS_Buttons();
    

    // Import *temp = new Import() ;
    // connect(temp, SIGNAL(after_import()),this, SLOT(slot_load_cell_dropdownlist()));

    }

    Import.h (child window)
    #ifndef IMPORT_H
    #define IMPORT_H

    #include <QDialog>

    namespace Ui {
    class Import;
    }

    class Import : public QDialog
    {
    Q_OBJECT

    public:
    explicit Import(QWidget *parent = 0);
    ~Import();

    private slots:
    void on_pushButton_import_config_clicked();

    void on_pushButton_import_dump_clicked();
    
    void on_pushButton_create_imported_config_clicked();
    

    signals:
    void after_import();

    private:
    Ui::Import *ui;
    };

    #endif // IMPORT_H

    import.cpp (child window)

    #include "import.h"
    #include "ui_import.h"
    #include <QFileDialog>
    #include <QMessageBox>
    #include "JailHouseCellList.h"
    #include "readfromdump/ReadDmp.h"
    #include "mainwindow.h"
    #include <iostream>

    Import::Import(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Import)
    {
    ui->setupUi(this);
    MainWindow *temp = new MainWindow() ;
    connect(this, SIGNAL(after_import()),temp, SLOT(slot_load_cell_dropdownlist()));

    }

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

    void Import::on_pushButton_import_config_clicked()
    {
    QString filepath = QFileDialog::getOpenFileName(this, tr("Import file"), "/home","Config_Files(*.info)") ;
    ui->lineEdit_config_File->setText(filepath);
    }

    void Import::on_pushButton_import_dump_clicked()
    {
    QString filepath = QFileDialog::getOpenFileName(this, tr("Import file"), "/home","Dump_Files(*.dmp)") ;
    ui->lineEdit_Dump_File->setText(filepath);
    }

    void Import::on_pushButton_create_imported_config_clicked()
    {
    try{
    std::string info_file = ui->lineEdit_config_File->text().toStdString() ;
    std::string dump_file = ui->lineEdit_Dump_File->text().toStdString() ;
    JailHouseCellList & JL = JailHouseCellList::getJailHouseCellList();
    ReadDmp jt(info_file,dump_file);
    JailHouseCell * jc = jt.get_cell();
    if( RootCell * j = dynamic_cast<RootCell * >(jc) )
    JL.add_rootcell_no_validation(*j);
    else
    JL.add_cell_no_validation(*jc);
    }
    catch(JailHouseCell_error & e)
    {
    std::string s = e.what();
    QMessageBox::warning(this,"Wrong input",e.what()) ;
    }
    catch(std::exception & e)
    {
    std::string s = e.what();
    QMessageBox::warning(this,"Wrong input",e.what()) ;
    }
    catch(...)
    {
    QMessageBox::warning(this,"unknownerror","unknownerror");
    }
    emit after_import();

    }

    //void Import::after_import() {
    // std::cout << "calling load_dropdownList() function" << std::endl ;
    //}

    Pls help me into this


  • Moderators

    @moyin said in how to call Mainwindow method from a childwindow.cpp:

    MainWindow *temp = new MainWindow() ;

    Why do you create a new MainWindow instance in Import constructor?!

    You should connect signals/slots in MainWindow where you create Import instance.
    This is completely wrong as you create a new Import instance:

    // Import *temp = new Import() ;
    // connect(temp, SIGNAL(after_import()),this, SLOT(slot_load_cell_dropdownlist()));
    

    It should be

    imp = new Import() ;
    connect(imp, SIGNAL(after_import()),this, SLOT(slot_load_cell_dropdownlist()));
    


  • @jsulm thnks for reply,

    I tried your sugesstion, but it does't resolve my issue.
    while debugging into emit after_import() , i found this below lines of code can u just clear me what does it mean.

    // SIGNAL 0
    void Import::after_import()
    {
    QMetaObject::activate(this, &staticMetaObject, 0, 0);
    }
    QT_END_MOC_NAMESPACE


  • Moderators

    @moyin Your problem is for sure not in this code. Either the signal is not emitted, or not connected to slot, or (like I suggested before) you actually use the wrong Import instance (as you create a new temporary instance which you connect then)...
    Can you show your code after applying my suggestion?



  • @jsulm thnks for rply,
    this is my mainwindow.cpp (constructor)

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {

    ui->setupUi(this);
    QFont font("Plastique",10);
    

    // font.setStyleHint(QFont::Monospace);
    QApplication::setFont(font);
    ui->tabWidget_Main->setHidden(true);

    ui->tabWidget_CELL_CONFIG->setHidden(true);
    ui->tableWidget_cell_config_2->setColumnWidth(4,250);
    Disable_Root_Tabs();
    Disable_LS_Buttons();
    

    // Import *temp = new Import() ;
    // connect(temp, SIGNAL(after_import()),this, SLOT(slot_load_cell_dropdownlist()));
    imp = new Import() ;
    connect(imp, SIGNAL(after_import()),this, SLOT(slot_load_cell_dropdownlist()));

    }



  • @moyin said in how to call Mainwindow method from a childwindow.cpp:

    @jsulm thnks for rply,
    this is my mainwindow.cpp (constructor)

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {

    ui->setupUi(this);
    QFont font("Plastique",10);
    

    // font.setStyleHint(QFont::Monospace);
    QApplication::setFont(font);
    ui->tabWidget_Main->setHidden(true);

    ui->tabWidget_CELL_CONFIG->setHidden(true);
    ui->tableWidget_cell_config_2->setColumnWidth(4,250);
    Disable_Root_Tabs();
    Disable_LS_Buttons();
    

    // Import *temp = new Import() ;
    // connect(temp, SIGNAL(after_import()),this, SLOT(slot_load_cell_dropdownlist()));
    imp = new Import() ;
    connect(imp, SIGNAL(after_import()),this, SLOT(slot_load_cell_dropdownlist()));

    }

    this is my import.cpp where i'm emitting signal.

    #include "import.h"
    #include "ui_import.h"
    #include <QFileDialog>
    #include <QMessageBox>
    #include "JailHouseCellList.h"
    #include "readfromdump/ReadDmp.h"
    #include "mainwindow.h"
    #include <iostream>

    Import::Import(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Import)
    {
    ui->setupUi(this);
    QFont font("Plastique",10);
    // font.setStyleHint(QFont::Monospace);
    QApplication::setFont(font);

    }

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

    void Import::on_pushButton_import_config_clicked()
    {
    QString filepath = QFileDialog::getOpenFileName(this, tr("Import file"), "/home","Config_Files(*.info)") ;
    ui->lineEdit_config_File->setText(filepath);
    }

    void Import::on_pushButton_import_dump_clicked()
    {
    QString filepath = QFileDialog::getOpenFileName(this, tr("Import file"), "/home","Dump_Files(*.dmp)") ;
    ui->lineEdit_Dump_File->setText(filepath);
    }

    void Import::on_pushButton_create_imported_config_clicked()
    {
    try{
    std::string info_file = ui->lineEdit_config_File->text().toStdString() ;
    std::string dump_file = ui->lineEdit_Dump_File->text().toStdString() ;
    JailHouseCellList & JL = JailHouseCellList::getJailHouseCellList();
    ReadDmp jt(info_file,dump_file);
    JailHouseCell * jc = jt.get_cell();
    if( RootCell * j = dynamic_cast<RootCell * >(jc) )
    JL.add_rootcell_no_validation(*j);
    else
    JL.add_cell_no_validation(*jc);
    }
    catch(JailHouseCell_error & e)
    {
    std::string s = e.what();
    QMessageBox::warning(this,"Wrong input",e.what()) ;
    }
    catch(std::exception & e)
    {
    std::string s = e.what();
    QMessageBox::warning(this,"Wrong input",e.what()) ;
    }
    catch(...)
    {
    QMessageBox::warning(this,"unknownerror","unknownerror");
    }
    emit after_import();

    }

    //void Import::after_import() {
    // std::cout << "calling load_dropdownList() function" << std::endl ;
    //}


  • Moderators

    @moyin Are you sure signal is emitted?



  • @jsulm thanks for rply,
    i dont how to confirm the signal is emitted or not but when i run the application in debug mode and step into emit after_import(); it takes me into this code snippet. that i already shared you.

    / SIGNAL 0
    void Import::after_import()
    {
    QMetaObject::activate(this, &staticMetaObject, 0, 0);
    }
    QT_END_MOC_NAMESPACE


  • Moderators

    @moyin You can just put a breakpoint in your slot. Or when you're in that code block then just go further (step) to see what is happening.
    Also do

    qDebug() << connect(imp, SIGNAL(after_import()),this, SLOT(slot_load_cell_dropdownlist()));
    

    to see whether the connection was actually successful.



  • @jsulm , yes! that is
    qDebug() << connect(imp, SIGNAL(after_import()),this, SLOT(slot_load_cell_dropdownlist()));
    returning true.

    And if i put BreakPoint in slot_load_cell_dropdownlist(). debugger not reaching to that BreakPoint.


  • Moderators

    @moyin Do you by any chance have more than one Import instance?
    And what are you doing in slot_load_cell_dropdownlist()?



  • @jsulm
    @ Do you by any chance have more than one Import instance?
    YES!
    Actually i have 2 import instance to the same pointer (imp) .

    @what are you doing in slot_load_cell_dropdownlist()?
    i am just reinitialising a Qcombobox.


  • Moderators

    @moyin said in how to call Mainwindow method from a childwindow.cpp:

    Actually i have 2 import instance to the same pointer (imp) .

    What?! Why? Then the first one is lost as you overwrite the pointer when you create the second instance. And which of them did you actually connected?



  • @jsulm Now i changed my code like,

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {

    ui->setupUi(this);
    QFont font("Plastique",10);
    

    // font.setStyleHint(QFont::Monospace);
    QApplication::setFont(font);
    ui->tabWidget_Main->setHidden(true);

    ui->tabWidget_CELL_CONFIG->setHidden(true);
    ui->tableWidget_cell_config_2->setColumnWidth(4,250);
    Disable_Root_Tabs();
    Disable_LS_Buttons();
    

    // Import *temp = new Import() ;
    // connect(temp, SIGNAL(after_import()),this, SLOT(slot_load_cell_dropdownlist()));
    imp2 = new Import(this) ;
    std::cout << "just before connect" << std::endl;
    qDebug() << connect(imp2, SIGNAL(after_import()),this, SLOT(slot_load_cell_dropdownlist()));
    std::cout << "just after connect" << std::endl;
    }



  • @jsulm
    Same result it is not calling slot_load_cell_dropdownlist() at all.



  • yeah! man its working thanks alot.


Log in to reply
 

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