DragonautX DragonautX - 29 days ago 8
C++ Question

How do I access the QLineEdit widget I added to this Qt example?

I followed this Qt tutorial on signals and slots, including the part where you delete the connection you can automatically make in Qt Designer and instead type in the

connect
function by hand in mainwindow.cpp. I wanted to make my own slot where moving the tutorial's QSlider changed a QLineEdit I added, so I did this:


  1. I added a QLineEdit widget named 'lineEdit' to my "mainwindow.ui" in QtDesigner.

  2. In the
    MainWindow
    class definition in mainwindow.h, I added this at the bottom of the class.

    public slots:
    void changeLineEdit() {
    ui->lineEdit->setText("Value was changed");
    }

  3. In the
    MainWindow
    constructor in mainwindow.cpp, I added this: (
    ui->horizontalSlider
    was the QSlider made in the tutorial.)

    connect(ui->horizontalSlider, SIGNAL(valueChanged(int)), SLOT(changeLineEdit()));



When I tried to build the project, I got 9 errors, one of them being "use of undefined type 'Ui::MainWindow'". What did I do wrong in this edit? The MainWindow class definition has a private pointer
Ui::Mainwindow *ui
, so I thought the slot definition would access the
ui
pointer and therefore the 'lineEdit' widget included there.

Answer

If you implement your slot in your header make sure you include the header for UI::MainWindow in your header for your class as well. Normally I would implement my slots in the cpp file instead however that is not a requirement.