【问题标题】:Why do I get non-object type 'char *(const char *, int) throw()' is not assignable为什么我得到非对象类型 'char *(const char *, int) throw()' is not assignable
【发布时间】:2020-09-29 00:35:00
【问题描述】:

我在下面实现了一个最小的可验证示例来复制该问题。 问题我的问题是在QLabel 倒计时到达00:00 之后,我希望应用程序自动关闭。但这没有发生,我从编译器获得以下错误:

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

在最小可验证示例代码下方

ma​​inwindow.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;
};

ma​​inwindow.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 格式。

我还咨询了thisthis,这些对我来说都是明确的例子,唯一的区别是我在这里使用的是 qt 组件,例如在这种情况下为 QLabel,这可能是一个简单的错误,但我有点糊涂了。

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

【问题讨论】:

  • 你有编译错误还是必须运行程序才能得到错误?
  • 什么是index?不管它是什么,你认为if (&amp;index) 应该是什么意思?
  • 我基本上是在尝试捕捉倒计时的00:00,如果是这样,请关闭应用程序。使用index,我试图抓住00:00
  • @GuillaumeRacicot,是的,我收到编译错误:我在问题上添加了错误

标签: c++ qt c++11 qt5


【解决方案1】:

你似乎有一些看起来像这样的代码:

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

Qt 文档说setText 返回void。换句话说,该函数什么也不返回。

但是,您尝试在 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();
    }
}

在这里,在每个滴答声中,我们测试时间是否等于零时间。如果是,我们继续执行关闭功能。

【讨论】:

  • 感谢您的猜测 1001% 正确!这正是我想要做的,你的建议奏效了。还要感谢您指出我正在调用一个空语句。我明白我做错了什么! :) 谢谢你的时间。非常感谢!
【解决方案2】:

您忘记将index 声明为变量。

在作用域的某个地方有一个函数index,你的编译器认为你正在尝试分配给它,你不能这样做。

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

我建议您查看库的 setText 文档以查看它返回的值,以及如何使用该值……如果它返回一个值(这对于 setter 来说会很奇怪)。

【讨论】:

  • 您好,感谢您阅读这个问题。我尝试根据您的建议进行修改,情况有所好转。我现在有一个小错误。我在显示我所做的问题上添加了 EDIT_2。我主要作用于构造函数
  • 我仍然没有看到名为 index 的变量的任何声明。
  • 对于声明,我最初尝试了这个:auto index = ui-&gt;countDown-&gt;setText("00:00"); 这给了我this 这就是为什么我试图有一个额外的方式
  • @Emanuele 这就是说setText 的返回类型为void,即它不返回任何东西。你希望index 是什么?要重新迭代,您需要查阅此setText 函数的文档以了解如何使用它。
  • 是的,当QLabelsetText00:00 时,基本上会捕捉到,这意味着当倒计时到达0(或我试图捕捉的索引)时。然后关闭应用程序。
猜你喜欢
  • 2015-01-05
  • 2020-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-08
  • 1970-01-01
相关资源
最近更新 更多