【QT】qt中不同类(class)的信号发送(emit)说明

哎哎9年前 (2015-03-26)C++4918

  QTableView的当前行改变时的信号Qt: QTableView的当前行改变时的信号

  void ShowLogDialog::showLogs(const QList<Log> &logs) {

  ModelBuilder::buildModel(&(this->model), logs);

  ui->tableView->setModel(this->model);

  ui->tableView->resizeColumnsToContents();

  connect(ui->tableView->selectionModel(),

  SIGNAL(currentRowChanged(const QModelIndex &, const QModelIndex &)),

  this, SLOT(showOperationDetails(const QModelIndex &, const QModelIndex &)));

  }

  当前行改变时, QTableView并不提供信号, 而是他的SelectionModel发出信号. 这个连接只有在setModel()后才有用, 当model改变后, 这个连接也会失去作用, 而要再新的model设置后再调用一次.

  在buildModel()时, 传入的model会被删除, 然后再为其创建一个新的model, 这时, 与tableView相关连的model被删除了, 所以他的SelectionModel也被删除, 与这个SelectionModel相关的连接也被自动删除, 所以在setModel()后, tableView会有一个新的SelectionModel, 也要对其进行新的信号糟连接.

  void ShowLogDialog::showOperationDetails(const QModelIndex & current,

  const QModelIndex & previous) {

  // 取得当前行的第四列的值: 操作详细信息, 然后显示到右边的QTextEdit中.

  QModelIndex index = ui->tableView->model()->index(current.row(), 3);

  ui->textEdit->setText(index.data().toString());

  }

  main中的类为A,另一个类为B

  B改变了某个值或发生了某个时间,发送一个信号,A通过connect连接检测到信号,然后运行对应的函数

 main.c other_class other_class=new other_class;

  添加一个连接

 connect(other_class,SIGNAL(other_class_signal()),this,SLOT(main_update()));

  main.h

  添加新类的头文件

 include <other_class.h> other_class.h

  在新类的头文件中添加信号的定义

 signals: void change(); other_class.c

  在新类的c文件的函数中发送信号

 emit change();

相关文章

解决QWidget: Cannot create a QWidget when no GUI is being used

  长时间不用Qt了,新建了个控制台工程再添加自己的类,即是不想使用designer来设计自己的界面,编译时没有错误,执行时出现QWidget: Cannot create a QWidget whe...

好久没更新了,这次介绍 qDebug()的所有输出转移至文件中

#include <QtDebug> #include <QFile> #include <QTextStream> #defi...