开发者

How do I declare objects from my main window in Qt Creator?

This question should hopefully be easy to answer. I created a few buttons in my MainWindow using Qt Creator, and when I go to write the functions for the buttons, the compiler says they were not declared in this scope. What do I need to #include for these objects to be declared? The compiler error for the following would be 'baseDir' was not declared in this scope. baseDir is the objectName for a lineEdit in my window.

mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include "ui_mainwindow.h"

    namespace Ui {
        class MainWindow;
    }

   class MainWindow : public QMainWindow
   {
        Q_OBJECT

    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();

    public slots:
        void getDir();
        void createProj();

    private slots:
        void on_findDir_clicked();

        void on_create_clicked();

    private:
        Ui::MainWindow *ui;
    };

    #endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "functions.h"
#include <QtGui/Q开发者_JAVA百科Application>
#include <QFileDialog>

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

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_findDir_clicked()
{
    QString path;

    path = QFileDialog::getOpenFileName(
        this,
        "Choose a file to open",
        QString::null,
        QString::null );

    baseDir->setText( path );
}


The items you define in your .ui file are not added directly to your main window class, they are added to its ui membre.

Try with:

ui->baseDir->setText( path );

Look at the ui_mainwindow.h file that is generated during the build if you're curious.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜