在Qt中传递父指针错误:没有在QObject

时间:2016-06-28 22:10:22

标签: c++ qt

我是qt的新手,我正在尝试创建一个程序,MainWindow调用QDialog输入一些数据。
  问题是我的QDialog中的parent()在我们的情况下没有任何访问MainWindow的公共方法

void save_city(const City *city); //public method of MainWindow

代码实际上很大,所以这里有一些代码。谢谢。

mainwindow.h

#include <QGraphicsScene>
#include <QGraphicsView>
#include <QMainWindow>
#include <QTextStream>
#include <QVector>
#include <QDebug>
#include <QFile>
#include "dialog_add_city.h"
#include "street.h"
#include "city.h"
#include "map.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    //main Window constructor
    explicit MainWindow(QWidget *parent = 0);
    //main Window deconstructor
    ~MainWindow();
    /*utility function to save the city
     *the function is public so that also QDialogs 
     *data saves can */
   //////////////
   //here is the public function
    void save_city(const City *city);


private slots:


private:
    Map mainMap;
    Ui::MainWindow *ui;
    QGraphicsView view;
    QGraphicsScene scene;
};

dialog_add_city.h

#ifndef DIALOG_ADD_CITY_H
#define DIALOG_ADD_CITY_H

#include <QDialog>
#include "mainwindow.h"
#include "City.h"

namespace Ui {
class Dialog_Add_City;
}

class Dialog_Add_City : public QDialog
{
    Q_OBJECT

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

private slots:
    //Add New City Button clicked-Adds an new city to our city_locations.txt
    void on_PushButton_Add_New_City_clicked();

private:
    Ui::Dialog_Add_City *ui;
};

#endif // DIALOG_ADD_CITY_H

dialog_add_city.cpp

#include "dialog_add_city.h"
#include "ui_dialog_add_city.h"

Dialog_Add_City::Dialog_Add_City(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog_Add_City)
{
    ui->setupUi(this);
}

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

void Dialog_Add_City::on_PushButton_Add_New_City_clicked()
{
    City *city=new City(ui->lineEdit_cityName->text(),
                        ui->lineEdit_X_Ko->text().toDouble(),
                        ui->lineEdit_Y_Ko->text().toDouble());
    qDebug() << ui->lineEdit_cityName->text()
             << ui->lineEdit_X_Ko->text()
             << ui->lineEdit_Y_Ko->text();
 /////////////////////////////
 //HERE IS THE PROBLEM   
 parent()->save_city(city);        
}

欢迎任何其他建议!

2 个答案:

答案 0 :(得分:0)

问题是parent()会将父对象的指针返回为QObject。

QObject Documentation

正如dreschrjm指出你可以尝试通过

来施放物体
MyWidget *myWidget = qobject_cast<MyWidget *>(obj);

from The Meta-Object System

答案 1 :(得分:0)

建议:使用Qt的信号/插槽机制来避免向后引用父。

Dialog_Add_City中将save_city声明为信号。 在MainWindow中将void MainWindow::show_add_city_dialog() { Dialog_Add_city dialog; connect(&dialog, &Dialog_Add_city::save_city, this, &MainWindow::save_city); dialog.exec(); } 声明为广告位

连接创建对话框的位置: e.g。

parent()->save_city(city);

Dialog_Add_City中的emit save_city(city);替换为emit save_city(ui->lineEdit_cityName->text(), ui->lineEdit_X_Ko->text().toDouble(), ui->lineEdit_Y_Ko->text().toDouble());

确保新的城市对象不会泄漏,我认为更好的设计是在其他地方创建City对象,而不是在对话框中。

所以

void MainWindow::save_city(const QString &cityName, double x, double y)
{
  City *city=new City(cityName, x, y);
  // do some interresting things with city.
}

和插槽

try {
$URL = 'http://example.com/webservices/security/accesscontrol.asmx';

$sc = new SoapClient(null, array(
  'location' => $URL,
  'uri' => 'http://example.com/webservices/security/',
  'trace' => 1
  ));

$usertoken = array('UserNameToken' =>
array(
  'UserName' => 'test',
  'Password' => 'test123'
));

$header = new SoapHeader('http://www.docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', 'Security', $usertoken);

$sc->__setSoapHeaders($header);

$test = $sc->__soapCall("AuthenticateClient",
  array(),
  array('soapaction' => 'http://example.com/webservices/security/AuthenticateClient')
);