1

我对 Qt 很陌生,但我想我需要使用信号和插槽来发送/接收数据,至少在教程和其他帖子中总是这样。

这应该有效:

按下Import按钮,选择 xls 文件 -> 在我们选择它之后。我们正在运行文件并为每个工作表获取相同的工作表名称和工作表表列标题(然后用户选择该列以及该列包含的所有内容;我们正在插入数据库等)

我想在我的“excel-2-some-db”模块中制作一个这样的 GUI 应用程序:

在此处输入图像描述

我上了两节课。第一个 - import_module:

导入模块.h:

#ifndef IMPORTDB_MODULE_H
#define IMPORTDB_MODULE_H

#include <QtGui/QMainWindow>
#include <QAxObject>
#include <QAxWidget>
#include "ui_importdb_module.h"
#include "headers_selection.h"//select form

class importdb_module : public QMainWindow
{
    Q_OBJECT

public:
    importdb_module(QWidget *parent = 0, Qt::WFlags flags = 0);
    ~importdb_module();

    //...

private:
    Ui::importdb_moduleClass ui;

    //...

    headers_selection* select_form;

public slots:
    void on_getExcelPath_clicked();
    void on_pushButton1_clicked();

    //signal we will send after read some sheets columns
signals:
    void sendTreeViewData(QString &sheet_name, QStringList &sheet_headers);

};

#endif // IMPORTDB_MODULE_H

导入模块.cpp

#include <QtGui>
 #include <QApplication>
#include "importdb_module.h"


importdb_module::importdb_module(QWidget *parent, Qt::WFlags flags)
    : QMainWindow(parent, flags)
{
    ui.setupUi(this);
    select_form = new headers_selection();

    //to show form on click
    connect(ui.pushButton1, SIGNAL(clicked()), select_form, SLOT(show()));

    connect(this, 
    SIGNAL(sendTreeViewData(QString &sheet_name, QStringList &sheet_headers)), 
    select_form, 
    SLOT(recieveTreeViewData(QString &sheet_name, QStringList &sheet_headers))); 

}

importdb_module::~importdb_module()
{

}

//....

//all file headers
void importdb_module::readSheetsHeaders(QAxObject* &_workbook, QAxObject* _worksheets, QAxObject* &_excel){
    QAxObject* sheet_i; 
    int sheets_count = _worksheets->property("Count").toInt(); //get how much lists there, gonna choose one to import data

    QString sheet_name;
    QStringList sheet_headers;//sheet headers will store here

    //sheets num starts from 1
    for(int i=1; i<= sheets_count; i++){
        sheet_i = _workbook->querySubObject("Worksheets(int)", i);//get teh list
        sheet_name = sheet_i->property("Name").toString();//get teh name

        //...
        getTableHeaders(sheet_i, sheet_headers);

        //says that we've send tree data into the form
        emit sendTreeViewData(sheet_name, sheet_headers);


        sheet_i->clear();
        sheet_headers.clear();
    }

    delete sheet_i;
};

//...

headers_selection.h

#ifndef HEADERS_SELECTION_H
#define HEADERS_SELECTION_H

#include <QWidget>
#include "ui_headers_selection.h"

class headers_selection : public QWidget
{
    Q_OBJECT

public:
    headers_selection(QWidget *parent = 0);
    ~headers_selection();

private:
    Ui::headers_selection ui;


public slots:
    void recieveTreeViewData(QString &sheet_name, QStringList &sheet_headers);


};

#endif // HEADERS_SELECTION_H

headers_selection.cpp

#include "headers_selection.h"

headers_selection::headers_selection(QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);
    QStandardItemModel *model = new QStandardItemModel;




}

headers_selection::~headers_selection()
{

}

void headers_selection::recieveTreeViewData(QString &sheet_name, QStringList &sheet_headers)
{
        //just test
QStandardItemModel *model = new QStandardItemModel;

model->setColumnCount(1);
model->setRowCount(5);
model->setData(model->index(0, 0), "some data0");
model->setData(model->index(1, 0), "some data1");
model->setData(model->index(2, 0), "some data2");
model->setData(model->index(3, 0), "some data3");
model->setData(model->index(4, 0), "some data4");

ui.treeView->setModel(model);
}

在我导入.xls并打开树视图选择窗口之后 - 它是空的!

所以我在这里遇到了两个可能的问题

也许sendTreeViewData没有调用recieveTreeViewData,或者我在里面的树视图做错了什么recieveTreeViewData

有人可以帮我解决它吗?

更新

好吧,我在代码中做了一些改动(感谢thomas_b,他的回答向我展示了方法),现在它可以工作了!

//impordb_module.h
signals:
    void sendTreeViewData(QString &sheet_name, QStringList &sheet_headers);

//heaqders_selection.h
public slots:
    void recieveTreeViewData(QString &sheet_name, QStringList &sheet_headers);

//heaqders_selection.cpp
void headers_selection::recieveTreeViewData(QString &sheet_name,  QStringList &sheet_headers)
{
    qDebug()<<sheet_name<<" gotcha! ";
}

//in impordb_module.cpp
importdb_module::importdb_module(QWidget *parent, Qt::WFlags flags): QMainWindow(parent, flags){

connect(this, SIGNAL(sendTreeViewData(QString &,QStringList &)), select_form, SLOT(recieveTreeViewData(QString &,QStringList &)));
}
//...

void importdb_module::readSheetsHeaders(QAxObject* &_workbook, QAxObject* _worksheets, QAxObject* &_excel){


        //...
        getTableHeaders(sheet_i, sheet_headers);

        //says that we've send tree data into the form
        emit sendTreeViewData(sheet_name, sheet_headers);
  //...
};
4

1 回答 1

2

可能由于连接语句中的参数名称,您的连接失败。尝试将其更改为:

connect(this, SIGNAL(sendTreeViewData(QString,QStringList)), 
    select_form, SLOT(recieveTreeViewData(QString,QStringList)));
于 2013-10-24T09:07:00.077 回答