两个对象QT之间的示例SLOT / SIGNAL

时间:2017-03-10 08:58:10

标签: c++ qt

我的应用程序包含2个不同的对象(QObject和QMainWIndow),我想知道如何使用SLOT / SIGNAL在它们之间进行通信。此外,现有更好的方法吗?

有人可以举一个简单的例子吗?谢谢:))

sample

mainwindow.h

// returns the data from a cell
public String getCellData(String sheetName,int colNum,int rowNum){
    try{
        if(rowNum <=0)
            return "";
    int index = workbook.getSheetIndex(sheetName);
    if(index==-1)
        return "";

    sheet = workbook.getSheetAt(index);
    row = sheet.getRow(rowNum-1);
    if(row==null)
        return "";
    cell = row.getCell(colNum);
    if(cell==null)
        return "";

    if(cell.getCellType()==Cell.CELL_TYPE_STRING)
        return cell.getStringCellValue();
    else if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC || cell.getCellType()==Cell.CELL_TYPE_FORMULA ){
        String cellText  = String.valueOf(cell.getNumericCellValue());
        if (HSSFDateUtil.isCellDateFormatted(cell)) {
           // format in form of M/D/YY
           double d = cell.getNumericCellValue();

           Calendar cal =Calendar.getInstance();
           cal.setTime(HSSFDateUtil.getJavaDate(d));
                cellText =
                    (String.valueOf(cal.get(Calendar.YEAR))).substring(2);
                    cellText = cal.get(Calendar.MONTH)+1 + "/" +
                               cal.get(Calendar.DAY_OF_MONTH) + "/" +
                               cellText;
                    }

                return cellText;
           }else if(cell.getCellType()==Cell.CELL_TYPE_BLANK)
                return "";
           else
                return String.valueOf(cell.getBooleanCellValue());
           }
           catch(Exception e){
                e.printStackTrace();
                return "row "+rowNum+" or column "+colNum +" does not exist  in xls";
           }
}

mainwindow.cpp

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "ui_mainwindow.h"
#include "object.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private:
    Ui::MainWindow *ui;

private slots:


};

#endif // MAINWINDOW_H

object.h

#include "mainwindow.h"
#include "object.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->chkState, SIGNAL(clicked()), this, SLOT(object->chkState();));
}

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

objet.cpp

#ifndef OBJET_H
#define OBJET_H

#include "mainwindow.h"
#include <QMainWindow>
#include <QObject>

class Object : public QObject
{
    Q_OBJECT
public:
    explicit Object(QObject *parent = 0);
    bool state;

signals:

private slots:
    void chkState(Ui::MainWindow *ui);
};

#endif // OBJET_H

2 个答案:

答案 0 :(得分:1)

您的代码中存在多个错误。 第一:

connect(ui->chkState, SIGNAL(clicked()), this, SLOT(object->chkState();));

在这里你说: &#34;当我们点击ui-&gt; chkState时,我希望你调用一个函数,即object-&gt; chkState slot&#34;。这绝对不是你想要的。 什么是对象?此对象尚未创建。你想要的是:

connect(ui->chkState, SIGNAL(clicked()), myobject, SLOT(chkState()));

使用myobject类型为Object的对象,因此您需要在mainwindow.h中添加一个

Object *myobject;

并在connect:

之前的mainwindow.cpp中
myobject = new Object(this);

此外,您的函数void chkState(Ui::MainWindow *ui);无法正常工作,因为您无法在参数中获取主窗口ui。 我建议你做什么,如果它仅用于测试,所以你知道父类是MainWindow的类型,你可以这样做:

void Object::chkState()
{
    MainWindow* parent = static_cast<MainWindow*>(parent());
    if (parent->ui->chkState->isChecked())
    {
        parent->ui->state->setText("true");
        state = true;
    }
    else
    {
        parent->ui->state->setText("false");
        state = false;
    }
}

因此,插槽中的参数将被删除。

答案 1 :(得分:0)

这是一个如何发射信号和插槽的简单示例。

        #ifndef MAINWINDOW_H
        #define MAINWINDOW_H

        #include <QMainWindow>
        #include "object.h"

        namespace Ui {
        class MainWindow;
        }

        class MainWindow : public QMainWindow
        {
            Q_OBJECT

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

        signals:
            void transmit_to_object(bool value);

        private slots:
            void receive_from_object(bool value);

            void on_checkBox_clicked();

        private:
            Ui::MainWindow *ui;
            object          m_object;
        };

        #endif // MAINWINDOW_H

    #ifndef OBJECT_H
    #define OBJECT_H

    #include <QObject>

    class object : public QObject
    {
        Q_OBJECT

        public:
            explicit object(QObject * parent = 0);

        signals:
            void transmit_to_gui(bool value);

        private slots:
            void receive_from_gui(bool value);

        private:
        bool state;

    };

    #endif // OBJECT_H

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    connect(&m_object,SIGNAL(transmit_to_gui(bool)),this,SLOT(receive_from_object(bool)));

    connect(this,SIGNAL(transmit_to_object(bool)),&m_object,SLOT(receive_from_gui(bool)));

}

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

void MainWindow::receive_from_object(bool value)
{
    if(value)
    {
        ui->lineEdit->setText("true");
    }
    else
    {
        ui->lineEdit->setText("false");
    }
}

void MainWindow::on_checkBox_clicked()
{
    if(ui->checkBox->isChecked())
    {
        emit transmit_to_object(true);
    }
    else
    {
        emit transmit_to_object(false);
    }

}

#include "object.h"
#include "mainwindow.h"

object::object(QObject *parent)
{

}

void object::receive_from_gui(bool value)
{
    state = value;

    emit transmit_to_gui(state);
}