c++ - QT信号和槽传递变量到另一种形式

标签 c++ forms qt connect qt-signals

我用谷歌搜索了 10 个小时,但没有任何帮助。

我在 QT 中有两种形式(.ui)。

第一个 mainwindow.ui - 我这里有表格,当用户点击表格行时,它将这个表格行的值发送到

第二个 zobraz.ui

主窗口.h

signals:
    void sendIntData(int data);

主窗口.cpp

void MainWindow::on_tableView_doubleClicked(const QModelIndex &index)
{
zobraz1 = new zobraz(this);

zobraz1->show();

int o=index.row();
QString oo=QString::number(o);
ui->textEdit->setText(oo);

emit sendIntData(o);

connect(this, SIGNAL(sendIntData(int)),zobraz1, SLOT(setIntData(int)));

}

zobraz.h

public slots:
    void setIntData(int data);

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

private:
    Ui::zobraz *ui;
    int indexx;

};

zobraz.cpp

void zobraz::setIntData(int data)
{
    indexx=data;

} 

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

    QString poradiee=QString::number(indexx);
    ui->label_2->setText(poradiee);

非常感谢您的回答,我不知道什么是坏的。

最佳答案

我没有测试您的代码,但有一个问题与连接有关:连接必须在发出信号之前完成。所以你应该改变这些行:

emit sendIntData(o);
connect(this, SIGNAL(sendIntData(int)),zobraz1, SLOT(setIntData(int)));

到:

connect(this, SIGNAL(sendIntData(int)),zobraz1, SLOT(setIntData(int)));
emit sendIntData(o);

关于c++ - QT信号和槽传递变量到另一种形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41912435/

相关文章:

c++ - 使用 CMake 混合 C 和 C++

javascript - 为什么我的 POST 变量没有在 this.form.submit(); 之后到达?

javascript - Magento 表单验证,表单不提交有效字段

c++ - 为什么我的删除按钮没有删除 QHBoxLayout 上的所有小部件

c++ - 在 Qt MDIWindow 中禁用恢复按钮

c++ - 避免在 unordered_map 插入中进行额外处理

c++ - 控制命名空间中的访问?

c++ - 为什么 chromium 实现了 Time::Now ?有什么好处?

forms - 如何重用Symfony2中遗漏的某些字段的表单

c++ - Qt 的 Pimpl 习惯用法