c++ - Qt 连接不工作

标签 c++ qt connect

这个程序应该采用基于QLineEdit的矩阵,然后在点击转置按钮后,它应该打开一个新的框架,其中转置矩阵作为QLabel`秒。

问题是,点击换位按钮没有做任何事情所以我认为它与连接有关

这是我的 .cpp 的头文件

#ifndef DIMENSIONS_H
#define DIMENSIONS_H

#include <QDialog>
#include <QLineEdit>

namespace Ui {
class Dimensions;
}

class Dimensions : public QDialog
{
    Q_OBJECT

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

private slots:
    void on_buttonBox_accepted();
    void accepts();

private:
    Ui::Dimensions *ui;
};


#endif // DIMENSIONS_H

这是我的代码:


void Dimensions::on_buttonBox_accepted()
{

rows=ui->spinBox->value();
columns=ui->spinBox_2->value();
QFrame *result = new QFrame;
QGridLayout* layout = new QGridLayout;

for(int i=0;i<rows;i++){
    for(int j=0;j<columns;j++){
        fields[i][j] = new QLineEdit();
        fields[i][j]->setFixedWidth(30);
        layout->addWidget(fields[i][j],i,j);
    }
}

transpose = new QPushButton("Transpose",result);
layout->addWidget(transpose, rows-1,columns,1,1);
result->setLayout(layout);
result->show();
connect(transpose,SIGNAL(clicked()),result,SLOT(accepts()));
}



void Dimensions::accepts()
{
QLabel *results[10][10];
QFrame *answer = new QFrame;
QGridLayout *RLayout = new QGridLayout;
for(int i=0;i<columns;i++){
    for(int j=0;j<rows;j++){
      results[i][j] = new QLabel(fields[j][i]->text());
      results[i][j]->setFixedWidth(30);
      RLayout->addWidget(results[i][j],i,j);
    }
}
answer->setLayout(RLayout);
answer->show();
}

最佳答案

我认为问题在于您将 receiver 定义为 result 对象,但是您的插槽位于 Dimensions 类中。所以你必须按如下方式设置连接:

connect(transpose, SIGNAL(clicked()), this, SLOT(accepts()));
//                                 ---^^^

关于c++ - Qt 连接不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26906551/

相关文章:

qt - QML 中宽度、高度和隐式宽度/高度与相应用例之间的差异

c++ - 显示黑色图像的几何变形

使用 OpenGL 进行 Qt 渲染

Linux - Qmake 编译错误 - 尽管安装了 'vorbisfile not found'

node.js - 如何在 Connect 中间件中使用异步初始化?

c - 传输端点未连接,连接失败()

c++ - 类 std::out_of_range 在哪里完全定义?

c++ - C和C++中将int值转换为char指针的区别

c++ - 当前面有元音时,代码不会将大写字母转换为小写字母

postgresql - 使用 SailsJS 在 PostgreSQL 中连接 session