Results 1 to 3 of 3

Thread: QVector filled with unespected numbers

  1. #1
    Join Date
    Oct 2015
    Posts
    1
    Qt products
    Qt4 Qt5

    Default QVector filled with unespected numbers

    Hi,
    I am creating a Qt application on raspberry pi. I have a serise of checkboxes that if they are checked add their given number (1-25) to a QVector named Bank_1. I decided to display elements of the Vector in a text editor to make sure they are what I expected. They were not. When I display Bank_1[0] the result is anywhere from 24 to 495192 (or similar). Below is my code.

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. QVector<int> Bank_1(QVector<int>(6));
    8. }
    9.  
    10.  
    11. void MainWindow::on_add_Relay_pushButton_clicked()
    12. {
    13.  
    14. if(ui->bank_Select_comboBox->currentText()=="Bank 1")
    15. {
    16.  
    17. Bank_1[0] = 1;
    18. }
    19. //right now I am trying to get the number to display but am still getting odd results
    20.  
    21. void MainWindow::displayVector(QVector<int> bank)
    22. {
    23. int i = bank[0];
    24.  
    25.  
    26. QString s = QString::number(i);
    27.  
    28. ui->relay_1_lineEdit->setText(s);
    29. }
    To copy to clipboard, switch view to plain text mode 

    If you need to see the .h file I can add it. Any help or explanation would be greatly appreciated.

  2. #2
    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: QVector filled with unespected numbers

    Do you initialize the vector somewhere?

    It is actually strange this works at all, the access of element in line 17 should have triggered an assert since your vector has size 0.

    Cheers,
    _

  3. #3
    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: QVector filled with unespected numbers

    Quote Originally Posted by cdbean04 View Post
    Any help or explanation would be greatly appreciated.
    The Bank_1 you declare and initialize in line 7 is a local variable defined in the constructor that will go out of scope when the constructor ends. Do you also have Bank_1 declared as a class member variable?
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

Similar Threads

  1. Chech if filled
    By Kamran Kawish in forum Newbie
    Replies: 6
    Last Post: 17th October 2012, 12:30
  2. sort a Qvector based on another Qvector
    By OzQTNoob in forum Qt Programming
    Replies: 2
    Last Post: 16th February 2012, 07:39
  3. Replies: 5
    Last Post: 3rd September 2011, 00:11
  4. How to get QGraphicsRectItem filled color
    By wisconxing in forum Qt Programming
    Replies: 1
    Last Post: 3rd February 2009, 03:28
  5. How to get QGraphicsItem's filled color
    By wisconxing in forum Newbie
    Replies: 1
    Last Post: 2nd February 2009, 15:48

Tags for this Thread

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.