Results 1 to 8 of 8

Thread: Need help with basic C++

  1. #1

    Default Need help with basic C++

    I need help again.

    I created a class "Count" there are some methods,argument etc.

    In MainWidnow I created table of Objects
    "
    Count number[15];

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

    }

    From MainWidnow object I created new object preview

    "
    void MainWindow:n_actionActionpreview_triggered()
    {

    preview = new Preview(this);
    preview->setModal(true);
    preview->get_path(&filename);
    preview->exec();

    }"

    I would like to see "number object" which I created in MainWindow from "preview". How to do it ?


    I hope I described my problem clearly.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Need help with basic C++

    Please use [code]...[/code] tags around code.

    Where is the numbers[] array declared?
    How have you tried to access it?
    How has that failed?

    Have you considered explicitly passing the number array to the preview object ?

  3. #3

    Default Re: Need help with basic C++

    I have the following Count class

    Qt Code:
    1. [U]count.h[/U]
    2.  
    3. class Count
    4. {
    5.  
    6. private:
    7.  
    8. bool table[19][8];
    9.  
    10. QString line
    11.  
    12. public:
    13.  
    14. int start_line,end_line,alarm_lines; start_line,end_line,alarm_lines;
    15. };
    To copy to clipboard, switch view to plain text mode 

    in MainWindow.cpp

    I create new objects "Count number [15]" this way

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "count.h"
    4.  
    5. Count number[15];
    6.  
    7. MainWindow::MainWindow(QWidget *parent) :
    8. QMainWindow(parent),
    9. ui(new Ui::MainWindow)
    10. {
    11.  
    12. ui->setupUi(this);
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 

    and in one of methods MainWindows I create new Object

    Qt Code:
    1. void MainWindow::on_actionActionpreview_triggered()
    2. {
    3.  
    4. preview = new Preview(this);
    5. preview->setModal(true);
    6. preview->exec();
    7.  
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 

    In the method void Preview:n_pushButton_clicked() I would like to change start_line in object number[0]
    Qt Code:
    1. #include "preview.h"
    2. #include "ui_preview.h"
    3. #include <QDebug>
    4. #include "mainwindow.h"
    5.  
    6. Preview::Preview(QWidget *parent) :
    7. QDialog(parent),
    8. ui(new Ui::Preview)
    9. {
    10.  
    11. ui->setupUi(this);
    12.  
    13.  
    14. }
    15.  
    16.  
    17. void Preview::on_pushButton_clicked()
    18. {
    19. number[0].start_line = 5;
    20. }
    To copy to clipboard, switch view to plain text mode 


    but during compilation I have the following error :" 'alarm' was not declared in this scope"


    What are the possibilities to change the start line for the number [0] ?

    Regards
    Artur

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Need help with basic C++

    The error message has nothing to do with your question.
    Where is the code that tries to use a variable/name "alarm"? The error message from the compiler will have the file and line number.

  5. #5
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Need help with basic C++

    Quote Originally Posted by arturs View Post
    I create new objects "Count number [15]" this way

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "count.h"
    4.  
    5. Count number[15];
    6.  
    7. MainWindow::MainWindow(QWidget *parent) :
    8. QMainWindow(parent),
    9. ui(new Ui::MainWindow)
    10. {
    11.  
    12. ui->setupUi(this);
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 
    Since you are allocating an array of your Count class outside of any class definition, you're are creating a global variable, which you should generally try to avoid doing. You should move that to make your Count array a member variable in your MainWindow class IMHO.

  6. #6

    Default Re: Need help with basic C++

    Sorry I made a mistake. I mean number not alarm and I would like to use it in the code

    Qt Code:
    1. #include "preview.h"
    2. #include "ui_preview.h"
    3. #include <QDebug>
    4. #include "mainwindow.h"
    5.  
    6. Preview::Preview(QWidget *parent) :
    7. QDialog(parent),
    8. ui(new Ui::Preview)
    9. {
    10.  
    11. ui->setupUi(this);
    12.  
    13.  
    14. }
    15.  
    16.  
    17. void Preview::on_pushButton_clicked()
    18. {
    19. number[0].start_line = 5;
    20. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Need help with basic C++

    The easiest way of having two classes share access to data is to pass a reference or pointer to that variable from the the object that created the variable to the other object.
    In your case passing a reference or point to "number" as a constructor argument of "Preview".

    Cheers,
    _

  8. #8

    Default Re: Need help with basic C++

    Could you give me an example this method and other method which are possible ?

Similar Threads

  1. Basic GUI Help
    By QT++ in forum Newbie
    Replies: 7
    Last Post: 20th August 2012, 04:58
  2. Need help with basic Qt
    By doforumda in forum Qt Programming
    Replies: 5
    Last Post: 10th July 2010, 08:53
  3. Need basic help
    By Ossi in forum Qwt
    Replies: 2
    Last Post: 23rd October 2009, 13:24
  4. Need immediate help on basic cpp
    By Sandip in forum General Programming
    Replies: 10
    Last Post: 21st September 2008, 10:33
  5. Qt/C++ basic Q-s
    By jamadagni in forum General Programming
    Replies: 9
    Last Post: 10th February 2006, 08:01

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.