【发布时间】:2020-09-29 00:35:00
【问题描述】:
我在下面实现了一个最小的可验证示例来复制该问题。
问题我的问题是在QLabel 倒计时到达00:00 之后,我希望应用程序自动关闭。但这没有发生,我从编译器获得以下错误:
non-object type 'char *(const char *, int) throw()' is not assignable
在最小可验证示例代码下方
mainwindow.h
#include <QMainWindow>
#include <QTime>
#include <QTimer>
#include <QProcess>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
void stopAtZeroCountDown();
public slots:
void timerUpdate();
private:
Ui::MainWindow *ui;
QTimer *timer;
QTime time;
QProcess *stopAtZero;
};
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
stopAtZeroCountDown();
ui->countDown->setText("1:00");
time.setHMS(0,1,0);
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(timerUpdate()));
timer->start(1000);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::stopAtZeroCountDown()
{
this->stopAtZero = new QProcess(this);
this->stopAtZero->setProcessChannelMode(QProcess::MergedChannels);
connect(this->stopAtZero, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
[this](int exitCode, QProcess::ExitStatus exitStatus){
qDebug() << "[EXEC] FINISHED: " << exitCode << exitStatus;
index = ui->countDown->setText("00:00"); // <-- Error here
if(&index)
{
this->stopAtZero->start(QStringLiteral("/bin/sh"), QStringList() << QStringLiteral("/path/to/shutdown_executable.sh"));
};
});
}
void MainWindow::timerUpdate()
{
time = time.addSecs(-1);
ui->countDown->setText(time.toString("mm:ss"));
}
下面是编译错误,还有here
/home/emanuele/catkin_docking_ws/src/lidarlauncher/src/lidarlauncher/mainwindow.cpp: In lambda function: /home/emanuele/catkin_docking_ws/src/lidarlauncher/src/lidarlauncher/mainwindow.cpp:113:40: error: could not convert ‘this->MainWindow::ui->Ui::MainWindow::<anonymous>.Ui_MainWindow::labelCountDown->QLabel::setNum(0)’ from ‘void’ to ‘bool’ if(ui->labelCountDown->setNum(00))
EDIT_2
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//ui->countDown->setText(time.toString("hh:mm:ss"));
ui->countDown->setText("1:00");
//ui->countDown->setText(time.toString("mm:ss"));
time.setHMS(0,1,0);
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(timerUpdate()));
timer->start(1000);
connect(this->stopAtZero, &QProcess::readyReadStandardOutput, [this, script = this->stopAtZero](){
QString s = QString::fromUtf8(script->readAll());
qDebug() << "[EXEC] DATA: " << s;
if (ui->countDown->setText("00:00")) { // <-- error here
this->stopAtZero->start(QStringLiteral("/bin/sh"), QStringList() << QStringLiteral("/home/emanuele/catkin_docking_ws/src/lidarboatsproject/stop_lidar_deck_and_gui.sh"));
}
});
}
到目前为止,我咨询了following source 来帮助我解决问题,但没有成功。
我知道问题是由应该是 QString 的分配引起的,因为我正在尝试捕获计时器的 00:00 格式。
我还咨询了this、this,这些对我来说都是明确的例子,唯一的区别是我在这里使用的是 qt 组件,例如在这种情况下为 QLabel,这可能是一个简单的错误,但我有点糊涂了。
感谢您指出解决此问题的正确方向。
【问题讨论】:
-
你有编译错误还是必须运行程序才能得到错误?
-
什么是
index?不管它是什么,你认为if (&index)应该是什么意思? -
我基本上是在尝试捕捉倒计时的
00:00,如果是这样,请关闭应用程序。使用index,我试图抓住00:00。 -
@GuillaumeRacicot,是的,我收到编译错误:我在问题上添加了错误