Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. Portuguese
  4. Declaração variavel
Qt 6.11 is out! See what's new in the release blog

Declaração variavel

Scheduled Pinned Locked Moved Portuguese
11 Posts 3 Posters 6.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wsoftware
    wrote on last edited by
    #1

    Senhores, boa noite. Uma dúvida de principiante no C++/QT, onde inicializar uma variável que é utilizada em dois métodos, na forma que coloco no código abaixo, dá o seguinte erro : mainwindow.cpp:51: error: 'databasePath' was not declared in this scope str = databasePath;

    INÍCIO DO CÓDIGO E CRIAÇÃO DA VARIAVEL databasePath

    #include "mainwindow.h"
    ...

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

    QString databasePath = "";
    

    ...

    MÉTODO QUE ESTÁ HAVENDO O ERRO

    void MainWindow::on_pushButton_clicked()
    {
    QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
    QMessageBox mb;
    QSqlDatabase myDB = QSqlDatabase::addDatabase("QSQLITE");
    if( !myDB.isValid() )
    {
    // This shows a message Box if Database Connection Fails.
    QString str;

        str = databasePath;      ////    ERRO AQUI 
    

    Tentei colocar a criação da variável antes do construtor, porém apresentou outros erros.

    ...
    QString databasePath = "";
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    ...

    Estou tentando executar este exemplo : http://qt-project.org/forums/viewthread/29855

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Rodrigocg
      wrote on last edited by
      #2

      vc esta no arquivo mainwindow.cpp, concerteza a variavel databasePath esta declarada no prototipo da classe no arquivo mainwindow.h.

      1 Reply Last reply
      0
      • W Offline
        W Offline
        wsoftware
        wrote on last edited by
        #3

        Rodrigo, como disse sou principiante e a variável não está declarada no mainwindow.h, onde é que informo ela ?

        #ifndef MAINWINDOW_H
        #define MAINWINDOW_H

        #include <QMainWindow>

        namespace Ui {
        class MainWindow;
        }

        class MainWindow : public QMainWindow
        {
        Q_OBJECT

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

        private slots:
        void on_pushButton_clicked();

        private:
        Ui::MainWindow *ui;
        };

        #endif // MAINWINDOW_H

        1 Reply Last reply
        0
        • W Offline
          W Offline
          wsoftware
          wrote on last edited by
          #4

          declarando a variável databasePath em mainwindow.h em public

          QString *databasePath;

          Agora está apresentando esta mensagem de erro : invalid conversion from 'QString*' to 'char' [-fpermissive], nesta linha abaixo.

          str = databasePath;

          se str também é QString o que poderia fazer ?

          1 Reply Last reply
          0
          • T Offline
            T Offline
            TioRoy
            wrote on last edited by
            #5

            Você não pode fazer QString = QString*. São coisas diferentes.

            @
            QString* databasePath;
            QString str;

            str = databasePath;
            @

            Declara o databasePath sem ponteiro (*);

            1 Reply Last reply
            0
            • W Offline
              W Offline
              wsoftware
              wrote on last edited by
              #6

              Legal TioRoy foi em cima, parece que está mais perto que longe agora. embora tenha dado outro errinho, vou procurar a solução :

              C:\desenvolvimento\c\projetos\sqlite\acesso\main.cpp:7: error: undefined reference to `MainWindow::~MainWindow()'

              Gostaria de enviar o projeto, caso alguém precise de um exemplo, alguém teria alguma sugestão ?

              1 Reply Last reply
              0
              • T Offline
                T Offline
                TioRoy
                wrote on last edited by
                #7

                O "corpo" do destrutor ( ~MainWindow() ) não deve estar declarado no CPP.

                Coloca no .h:
                ~MainWindow() { }

                1 Reply Last reply
                0
                • W Offline
                  W Offline
                  wsoftware
                  wrote on last edited by
                  #8

                  TioRoy, desculpe minha ignorância, ainda estou aprendendo. tenho dois .cpp's o main.cpp e mainwindow.cpp, de qual eu deverei tirar ?

                  main.cpp está assim :

                  int main(int argc, char argv[])
                  { QApplication a(argc, argv);
                  MainWindow
                  w = new MainWindow();
                  w->show();
                  return a.exec();
                  }

                  o meu mainwindow.h está assim :

                  #ifndef MAINWINDOW_H
                  #define MAINWINDOW_H
                  #include <QMainWindow>

                  namespace Ui {
                  class MainWindow;
                  }

                  class MainWindow : public QMainWindow
                  {
                  Q_OBJECT

                  public:
                  explicit MainWindow(QWidget *parent = 0);
                  QString databasePath;
                  QString tmpString;
                  QString str;
                  ~MainWindow();

                  private slots:
                  void on_pushButton_clicked();

                  private:
                  Ui::MainWindow *ui;
                  };

                  #endif // MAINWINDOW_H

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    TioRoy
                    wrote on last edited by
                    #9

                    Coloca no mainwindow.cpp

                    @
                    MainWindow::~MainWindow()
                    {

                    }
                    @

                    PS: Para facilitar a leitura no forum, quando for postar um código, coloque o caracter @ antes e depois do código. Fica mais fácil de ler.

                    1 Reply Last reply
                    0
                    • W Offline
                      W Offline
                      wsoftware
                      wrote on last edited by
                      #10

                      Valeu TioRoy, não conhecia esta do arroba.

                      Apresentou este erro agora, preciso fazer alguma alteração no main.ccp ?

                      C:\desenvolvimento\c\projetos\sqlite\acesso\main.cpp:10: error: undefined reference to `MainWindow::MainWindow(QWidget*)'

                      @
                      int main(int argc, char argv[])
                      {
                      QApplication a(argc, argv);
                      MainWindow
                      w = new MainWindow(); // ERRO AQUI
                      w->show();
                      return a.exec();
                      }
                      @

                      1 Reply Last reply
                      0
                      • T Offline
                        T Offline
                        TioRoy
                        wrote on last edited by
                        #11

                        No main.cpp você tem a linha

                        @
                        #include "mainwindow.h"
                        @
                        ?

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved