为什么我得到非对象类型 'char *(const char *, int) throw()' is not assignable

Why do I get non-object type 'char *(const char *, int) throw()' is not assignable

我在下面实施了一个最小的可验证示例来复制该问题。 我遇到的问题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"));             
         }
    });
}

目前我请教了帮我解决了问题,但没有成功。 我知道问题是由一个应该是 QString 的赋值引起的,因为我正在尝试捕获计时器的 00:00 格式。

我也参考了, this,这些对我来说都是很清楚的例子,唯一的区别是我在这里使用的是qt组件,比如在这个例子中是一个QLabel,它可能是一个简单的错误,但我有点困惑。

感谢您指出解决此问题的正确方向。

您忘记将 index 声明为变量。

范围内某处有一个函数 index,您的编译器认为您正在尝试分配给它,但您不能这样做。

此外,if (&index) 没有任何意义,因为存在的每个变量都有一个非空地址。

我建议您查看图书馆的文档以了解 setText 的价值 returns,以及如何使用该价值……如果它 returns 是一个价值(这将setter).

很奇怪

您似乎有一些代码如下所示:

如果您 Qt documentation for QLabel::setText 阅读正确,您会发现您提供的代码存在问题。

Qt 文档说 setText returns void。也就是说,那个函数return什么都没有。

然而,您尝试获取要在 if 语句中测试的函数的结果:

//          call to setText
//  v-----------------------------v
if (ui->countDown->setText("00:00")) {
    // ...            
}

编译器看到的是这样的:

if (/* expression that returns void */) {
    // ...            
}

这是无效的。你不能做 if (void())。它甚至会做什么?

无论您尝试使用此 if 语句测试什么,都必须以其他方式完成。


我会尝试猜测您要为以下内容做什么,因此它可能不太准确。

我的猜测是您正在尝试测试倒计时是否等于 00:00 以便关机。让我来帮你。

您想在计时器结束时执行某项操作。这应该在您的计时器更新成员函数中完成:

void MainWindow::timerUpdate()
{
    time = time.addSecs(-1);
    ui->countDown->setText(time.toString("mm:ss"));

    // Check if we have a zero time on this tick
    if (time == QTime(0, 0)) {
        // put your code into the shutdown_function to make the shutdown
        shutdown_function();
    }
}

在每个滴答中,我们测试时间是否等于零时间。到时,我们继续执行关机功能。