Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Ui not in scope

    General and Desktop
    2
    8
    1773
    Loading More Posts
    • 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.
    • D
      davidrhcp last edited by

      This is my code for main.cpp
      @
      #include "mainwindow.h"
      #include <QApplication>
      #include <cstdlib>
      #include <QNetworkConfiguration>
      #include <QDebug>
      #include <QDir>
      #include <QFileInfo>
      #include <QNetworkAccessManager>
      #include <QNetworkConfiguration>
      #include <mainwindow.h>
      #include <QFile>
      #include <QString>
      #include <QIODevice>
      #include <QtGui>

      void Write(QString MBW){

      QFile mFile(MBW);

      if (mFile.open(QIODevice::WriteOnly))
      {
      QTextStream out(&mFile);
      out << ui->lineEdit->text();
      }

      }
      int main(int argc, char *argv[])
      {

      QApplication a(argc, argv);
      MainWindow w;
      w.show();
      
      // creating directory upon run for user input
      QDir mDir;
      mDir.mkpath("C:/Monice/BW");
      
      
      QString mFile = ("C:/Monice/BW/MBW.txt");
      
      return a.exec(&#41;;
      

      }
      @
      The ui apparently is not in the declared in the scope
      not sure what to do

      [andreyc EDIT]: Added '@' around the code. Please use '@' around your source code, log output, etc.

      1 Reply Last reply Reply Quote 0
      • A
        andreyc last edited by

        What are you trying to achieve?

        ui is a member of MainWindow class and is not accessible from standalone function void Write(QString MBW)

        1 Reply Last reply Reply Quote 0
        • D
          davidrhcp last edited by

          im trying to get a push button to save the text entered by a user into a line edit box to save the text to a file in a directory. this is the main.cpp

          @#include "mainwindow.h"
          #include <QApplication>
          #include <cstdlib>
          #include <QNetworkConfiguration>
          #include <QDebug>
          #include <QDir>
          #include <QFileInfo>
          #include <QNetworkAccessManager>
          #include <QNetworkConfiguration>
          #include <mainwindow.h>
          #include <QFile>
          #include <QString>
          #include <QIODevice>
          #include <QtGui>

          void Write(QString MBW){

          QFile mFile(MBW);

          if (mFile.open(QIODevice::WriteOnly))

          {
          QTextStream out(&mFile);
          out << ui->lineEdit->text();
          }

          }
          int main(int argc, char *argv[])
          {

          QApplication a(argc, argv);
          MainWindow w;
          w.show();
          
          // creating directory upon run for user input
          QDir mDir;
          mDir.mkpath("C:/Monice/BW");
          
          
          QString mFile = ("C:/Monice/BW/MBW.txt");
          
          return a.exec&#40;&#41;;
          

          }
          @

          This is the main window.cpp

          @#include "mainwindow.h"
          #include "ui_mainwindow.h"
          #include "QObject"
          #include "QtCore"
          #include "QNetworkConfigurationManager"
          #include "QNetworkInterface"
          #include "QHostAddress"
          #include "QString"
          #include "QFile"
          #include "QLineEdit"
          #include "QDir"
          #include "QApplication"
          #include "QMainWindow"

          MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)

          {
          ui->setupUi(this);

          //display network interfaces
          QList<QNetworkInterface> list = QNetworkInterface::allInterfaces();
          
          // each interface is allocated one after the other in a list
          

          foreach (QNetworkInterface mon, list)

          //adding the list to be shown in the interface combobox
          {
          ui->comboBox_Interface->addItem(mon.name());
          }
          
          // a for loop that counts from 50-90 in multiples of 5
          for (int i = 50; i <= 90; i += 5)
          //selecting the combox alert to diplay the figures 50-90 with a % symbol
          {
              ui->comboBox_Alert->addItem(QString::number(i) + "%");
          }
          

          }

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

          QFile::MBW()
          {
          mfile;
          }

          void MainWindow::on_save_clicked()
          {

          }
          @

          and this is the header

          @#ifndef MAINWINDOW_H
          #define MAINWINDOW_H

          #include <QMainWindow>
          #include <QString>
          #include <QObject>
          #include <QFile>
          namespace Ui {
          class MainWindow;
          }

          class MainWindow : public QMainWindow
          {
          Q_OBJECT

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

          private slots:

          void on_save_clicked();
          

          private:
          Ui::MainWindow *ui;
          };

          #endif // MAINWINDOW_H
          @

          [edit: added missing coding tags @ SGaist]

          1 Reply Last reply Reply Quote 0
          • A
            andreyc last edited by

            Please add '@' around your code to make it better readable.

            You use private variable of a class MainWindow in standalone function. It is not going to work in C++.

            Make Write() a member of MainWindow
            or
            provide a getter function in MainWindow for ui and use it in Write.

            1 Reply Last reply Reply Quote 0
            • D
              davidrhcp last edited by

              thanks
              how would i make the write() a function. a lot of QT documents is about the Q strings and so fourth. i dont even understand members, functions and classes, could you suggest a link that may help? also sorry about the code thing, how do i add @ around the code?

              1 Reply Last reply Reply Quote 0
              • A
                andreyc last edited by

                [quote author="davidrhcp" date="1399404981"]i don't even understand members, functions and classes, could you suggest a link that may help? [/quote]
                I would suggest to read some books about C++ to get more info about classes, etc
                "Here is good list of the books":http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list
                "Online C++ tutorial":http://www.learncpp.com/
                "another C++ tutorial":http://www.cplusplus.com/doc/tutorial/

                [quote author="davidrhcp" date="1399404981"]also sorry about the code thing, how do i add @ around the code?[/quote]
                To make your code looks pretty just put character "@" before the code and after.
                Like below but without quotation marks
                "@"
                int main()
                {
                }
                "@"

                1 Reply Last reply Reply Quote 0
                • A
                  andreyc last edited by

                  In your example try to delete function Write() from main.cpp
                  and put its content into void MainWindow::on_save_clicked()

                  @
                  void MainWindow::on_save_clicked()
                  {
                  // creating directory upon run for user input
                  QDir mDir;
                  mDir.mkpath("C:/Monice/BW");

                  QString MBW = ("C:/Monice/BW/MBW.txt");
                  
                  QFile mFile&#40;MBW&#41;;
                  
                  if (mFile.open(QIODevice::WriteOnly))
                  {
                      QTextStream out(&mFile);
                      out << ui->lineEdit->text();
                  } 
                  

                  }
                  @

                  1 Reply Last reply Reply Quote 0
                  • D
                    davidrhcp last edited by

                    thank u, you have been an awesome help! some good sources there as well really appreciated

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post