简体   繁体   中英

Signals/slots Qt5 C++

I have established a connection with the slot, the function is called, and the values ​​via qDebug () are output, but the table does not change, what is wrong?

mainwindow.cpp

<pre>MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{

    ui->setupUi(this);

   ui->tableWidget->horizontalHeader()->hide();
   ui->tableWidget->verticalHeader()->hide();

    //Matrix *matr=new Matrix;
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_updateTbl(int **mas, int n){
    for(int i=0;i<n;i++){
        ui->tableWidget->insertRow(ui->tableWidget->rowCount());
        for(int j=0;j<n;j++){
            ui->tableWidget->insertColumn(ui->tableWidget->columnCount());

            ui->tableWidget->setItem(i,j,new QTableWidgetItem( QString::number(mas[i][j])));
        }
    }
}

</pre>

main.cpp

<pre>
#include "mainwindow.h"
#include <QApplication>
#include "matrix.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    Matrix matr;
    MainWindow mywnd;

    QObject::connect(&matr,SIGNAL(updateTbl(int**,int)), &mywnd, SLOT(on_updateTbl(int**,int)));

    matr.upTable();

    return a.exec();
}
</pre>

matrix.cpp

<pre>
#include "matrix.h"

#include <QFile>
#include <QDebug>
#include <QString>
#include <QTextStream>

Matrix::Matrix()
{
    QFile file("mas.txt");

    this->mas=alloc_mem(n,n);


    array_to_file(file,n,n);
    fill_array(file,mas,n,n);

    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if (mas[i][j]!=mas[n-j-1][n-i-1]) {
                symmetrical=false;
                break;
            }
        }
    }

    if(symmetrical){
        for(int i=0;i<n;i++){
            mas[i][i]=0;
            mas[i][n-i-1]=0;
        }
        //print(mas,n,"Измененная");
    }

    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(mas[i][j]) vec.append(mas[i][j]);
        }

    }

}

int** Matrix::alloc_mem(int height, int width){
    int** mas = new int*[height];
    for (int i = 0; i < height; i++) {
        mas[i] = new int[width];
    }
    return mas;
}

void Matrix::array_to_file(QFile &file, int height, int width){
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
        return;
    int to_mas;
    QTextStream out(&file);
    for (int i = 0; i < height; i++){
        for (int j = i + 1; j < width; j++){
            to_mas=rand()%100-50;
            out<<to_mas<<" ";
        }
        out<<1<<" ";
        out<<"\n";
    }
    file.close();
}

void Matrix::fill_array(QFile &file, int **mas, int height, int width){
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return;
    QTextStream in(&file);
    QStringList matr = in.readAll().split("\n");
    for (int i = 0; i < height; i++){
        for (int j = 0; j < width-i; j++){
            mas[i][j]=matr.at(i).split(" ").at(j).toInt();
            mas[height-1-j][width-1-i]=mas[i][j];
        }
    }

    file.close();
}

void Matrix::print(int **mas, int n, QString name){
    for(int i=0;i<n;i++){

        for(int j=0;j<n;j++){

        }
    }
}

void Matrix::upTable(){
    emit updateTbl(mas,n);
}
</pre>

mainwindow.h

<pre>
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private:
    Ui::MainWindow *ui;
private slots:
    void on_updateTbl(int**,int);
};

#endif // MAINWINDOW_H
</pre>

matrix.h

<pre>
#ifndef MATRIX_H
#define MATRIX_H

#include <QVector>
#include <QFile>
#include <QString>
#include <QObject>

class Matrix:public QObject
{
    Q_OBJECT

public:
    Matrix();
    ~Matrix(){}
    //bool check_symmetrical(int **mas,int n);
    void print_vector(QVector<int> vector);
    int** alloc_mem(int height,int width);
    void array_to_file(QFile& file,int height,int width);
    void fill_array(QFile& file,int **mas,int height,int width);
    void print(int** mas,int n,QString name);
    void upTable();
private:
    int n=10;
    int **mas;
    bool symmetrical=true;
    QVector<int> vec;
signals:
    void updateTbl(int**,int);

};

#endif // MATRIX_H
</pre>

If I copy this code into the constructor, then everything is fine and everything changes, but nothing works from the function

How do you want to see table with values when widget with updated table is not displayed?

MainWindow w;
w.show();    // <--- w is displayed, tableWidget is not modified in this widget

Matrix matr;
MainWindow mywnd; // <--- mywnd is not displayed

QObject::connect(&matr,SIGNAL(updateTbl(int**,int)), &mywnd, SLOT(on_updateTbl(int**,int)));
                                                   // mywnd is updated 
matr.upTable();

add

mywnd.show();

you will see the other widget with updated content.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM