1

I'm new to coding with QTCreator, and Linux Graphical Development in general. What is the, probably trivial, thing I am doing wrong here?

I could write this code and make it work easily without using the form method, but I'd really prefer to use the graphical interface, as it should be faster (once I know what compiler wants me to do).

When I enter this code it tells me that btnStopGo isn't defined in this context. I used the forms interface to drop the button onto a form.

The code is in the file mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

...

void MainWindow::on_btnStopGo_clicked()
{
    if (bDoStream){
        if (bStreaming){
            //TODO: Send stop stream signal, save stream to file, dispose stream
            bStreaming = false;
            btnStopGo->setText("Go");
        }
        else{
            btnStopGo->setText("Stop");
            bStreaming = true;
            // TODO: request a stream of quotes
        }
    }
    else {
        // TODO: Get a single quote tick
    }

}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT;

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    ...

private slots:
    ...

    void on_btnSelect_clicked();

    void on_btnStopGo_clicked();

private:
    Ui::MainWindow *ui;

};
#endif // MAINWINDOW_H

Other visible files in the project are : Hello2.pro, main.cpp, and mainwindow.ui. main.cpp declares mywindow as the variable to access the main window. I've tried using "ui.btnStopGo", mywindow.btnStopGo, ui->btnStopGo (blows up the compiler), and every other combo I can think of. None of them work so far.

I don't know if this is relevant but I'm using: Qt Creator 4.11.0 Based on Qt 5.12.8 (GCC 9.3.0, 64 bit) on a Linux Mint 20 Cinnamon with KDE added manually.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
CapnJack
  • 15
  • 5
  • `ui_mainwindow.h` is automatically generated from `mainwindow.ui` by `uic`. In it there is a definition of a class `Ui_MainWindow`. You should add it as a member of your class (`MainWindow`) and call `m_ui.setupUi(this);` in your constructor. Then you can access `btnStopGo` like this: `m_ui.btnStopGo` – Osyotr Jan 21 '22 at 21:19
  • How do I get uic to run? The file isn't in the project directory. I have compiled and run this project several times. I'm now trying to add the functionality behind the UI. There is no man or info page for uic. – CapnJack Jan 21 '22 at 21:29
  • You likely forgot a line in your .pro file, see [this answer](https://stackoverflow.com/a/20589793/1719926). – sigma Jan 21 '22 at 22:49
  • No the FORMS line is in the .pro file. when I try to use ui.btnStopGo it tells me the button is a pointer (which it is based on the ui_mainwindow.h, which the compiler is creating in a tmp directory) and suggests the form ui->btnStopGo which throws an undefined reference error in qt_static_metacall in moc_mainwindow.cpp which isn't a file that is part of the project. – CapnJack Jan 21 '22 at 23:08

1 Answers1

0

If you put the button in the form designer, and called the button btnStopGo, then it ends up compiled into the ui_mainwindow.h and .cc files. Your MainWindow class, which you wrote yourself, doesn't have a (member) variable called btnStopGo. It does have a variable ui, which points to the user interface which was generated from the form designer.

Use ui->btnStopGo->setText("stop") instead.

  • Actually the MainWindow class was created by QtCreator. When using ui->btnStopGo->setText("stop") I get this error: moc_mainwindow.o: in function `MainWindow::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)' error: undefined reference to `MainWindow::on_rbtnOnce_clicked(bool)' This is at line 95 which is `case 2: _t->on_rbtnOnce_clicked((*reinterpret_cast< bool(*)>(_a[1]))); break;` Additional error = collect2: error: ld returned 1 exit status – CapnJack Jan 21 '22 at 23:46
  • Thanks to Genjutsu for the initial help and Adrian for the correct answer. Somehow the rbtnOnce radio button code got corrupted. I deleted the button and all referenced code, and recreated it. Now the application compiles and runs properly. – CapnJack Jan 22 '22 at 00:45