【问题标题】:QT signal and slot pass variable to another formQT信号和槽将变量传递给另一种形式
【发布时间】:2017-01-28 17:04:53
【问题描述】:

我在谷歌上搜索了 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);

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

【问题讨论】:

    标签: c++ forms qt connect qt-signals


    【解决方案1】:

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

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

    到:

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

    【讨论】:

    • 我试过了,还是没有,当我点击表中的指定行时,它仍然没有将带有行号的 int 变量发送到 zobraz.cpp label_2。它已编译但连接不起作用,变量“indexx”与变量“o”不同
    • 抱歉,我现在无法测试您的代码。要调试您的代码,请尝试添加 qDebug()
    【解决方案2】:

    setintdata 的问题 vas。它应该看起来像这样。但是 IDK 为什么。

    void zobraz::setIntData(int data)
    {
    
        QString poradieee=QString::number(data+1);
        ui->label_2->setText(poradieee);
    
        indexx=data;
    
    }
    

    【讨论】:

    • 因为调用indexx=data;只会更新indexx的内容。标签没有理由更新,因为您正在为变量分配新值。
    猜你喜欢
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 2012-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多