对按钮事件执行功能

execute function on pushbutton event

本文关键字:功能 执行 事件 按钮      更新时间:2023-10-16

这里完全是初学者。每当按下pushButton时,我都试图执行一个函数,但它只有在关闭主窗口后才能执行。我怎样才能在按下按钮时立即实现这一点?

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
using namespace std;
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}
MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::on_pushButton_clicked()
{
    cout << "test";
}

"测试"仅在窗口关闭后打印。当我调用自己的函数时,也会发生同样的事情。

如果您只是调试/测试,并且您不打算让您的最终应用程序使用它,您可以尝试以下操作:

#include <QDebug>
    void MainWindow::on_pushButton_clicked()
    {
        qDebug() << "test";
    }

try:

void MainWindow::on_pushButton_clicked()
{
    std::cout << "test" << std::endl;
}

通过提供endl;set:cout缓冲区被刷新,您也可以添加std::cout << std::flush;以刷新缓冲区。

此外,请避免使用命名空间std;