这个程序应该采用基于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/