Results 1 to 4 of 4

Thread: Passing variable from one form to another

  1. #1
    Join Date
    Nov 2016
    Posts
    5
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Passing variable from one form to another

    Hello, I am having trouble passing a value from one function in Qt to another in a different form

    Beastiary.cpp
    Qt Code:
    1. void beastiary::on_listWidget_itemClicked(QListWidgetItem *item)
    2. {
    3. const QString nvalue = ui->listWidget->currentItem()->text(); //Value that needs to passed
    4. read red;
    5. red.setModal(true);
    6. red.exec();
    7. red.ulk();
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 

    read.cpp
    Qt Code:
    1. void read::ulk()
    2. {
    3.  
    4. QFile file (nvalue); //needs nvalue from beastiary so it knows the name of the file to look for
    5. if (!file.open(QFile::ReadOnly | QFile::Text))
    6. {
    7. QMessageBox::warning (this, "Error", "File Could not Be Opened");
    8.  
    9. }
    10. QString line;
    11. QTextStream in(&file);
    12. while (!in.atEnd())
    13. {
    14. line = in.readLine();
    15. ui->lineEdit_name_r->setText(ui->lineEdit_name_r->text()+line);
    16. qDebug() <<line;
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

    The variable nvalue must be obtained in beastiary.cpp but is needed in read.cpp and im not sure of how to pass the variable to read from beastiary.

  2. #2
    Join Date
    May 2015
    Posts
    9
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Passing variable from one form to another

    Hi.
    In read.cpp or read.h add variable nvalue (if you like the same name), now in beastiary.cpp before red.exe() assign value to "nvalue", for example: red.nvalue = nvalue.

    Hope this help.

  3. #3
    Join Date
    Nov 2016
    Posts
    5
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Passing variable from one form to another

    Thanks for the reply, I tried your suggestion but got the following error: passing 'const QString' as 'this' argument discards qualifiers [-fpermissive]

    This is the code I wrote for it
    read.h
    Qt Code:
    1. #ifndef READ_H
    2. #define READ_H
    3.  
    4. #include <QDialog>
    5.  
    6.  
    7. namespace Ui {
    8. class read;
    9. }
    10.  
    11. class read : public QDialog
    12. {
    13. Q_OBJECT
    14.  
    15. friend class beastiary;
    16.  
    17. public:
    18. const QString nvalue;
    19. void ulk();
    20. explicit read(QWidget *parent = 0);
    21. ~read();
    22.  
    23. private:
    24. Ui::read *ui;
    25.  
    26. };
    27.  
    28.  
    29. #endif // READ_H
    To copy to clipboard, switch view to plain text mode 

    beastiary.cpp
    Qt Code:
    1. void beastiary::on_listWidget_itemClicked(QListWidgetItem *item)
    2. {
    3. const QString nvalue = ui->listWidget->currentItem()->text();
    4. read red;
    5. red.nvalue=nvalue;
    6. red.setModal(true);
    7. red.exec();
    8. red.ulk();
    9.  
    10. }
    To copy to clipboard, switch view to plain text mode 

    read.cpp
    Qt Code:
    1. void read::ulk()
    2. {
    3. QFile file (nvalue);
    4. if (!file.open(QFile::ReadOnly | QFile::Text))
    5. {
    6. QMessageBox::warning (this, "Error", "File Could not Be Opened");
    7. }
    8. QString line;
    9. QTextStream in(&file);
    10. while (!in.atEnd())
    11. {
    12. line = in.readLine();
    13. ui->lineEdit_name_r->setText(ui->lineEdit_name_r->text()+line);
    14. qDebug() <<line;
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    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: Passing variable from one form to another

    You can't assign to a "const" member or variable, it is "constant".

    You can keep the const if you pass the value as an argument to read's constructor.

    Cheers,
    _

Similar Threads

  1. Passing variable to stylesheet in runtime
    By ndev in forum Qt Programming
    Replies: 5
    Last Post: 23rd February 2015, 12:58
  2. Passing variable from tab to tab in QTabWidget
    By saad_saadi in forum Qt Programming
    Replies: 1
    Last Post: 3rd January 2014, 08:04
  3. Pass variable from a form to another form
    By dark1988 in forum Qt Programming
    Replies: 5
    Last Post: 8th February 2011, 18:19
  4. connect() variable passing issue
    By hojoff79 in forum Newbie
    Replies: 6
    Last Post: 1st February 2011, 11:18
  5. Replies: 3
    Last Post: 8th June 2007, 20:13

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.