Results 1 to 2 of 2

Thread: Changing text label depending on combobox selection

  1. #1
    Join Date
    May 2012
    Posts
    7
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Changing text label depending on combobox selection

    Hi,
    I have a problem that is probably very simple, but I don't know how to do it.
    I have a basic contacts book where essentially there is a combo box that you can use to scroll through names and once a name is selected it should change a text box with the persons phone number. The problem it all seems to work exept the query of the phone number.

    QString *phone = ui->cmbMembers->currentText();
    phone = "SELECT phone FROM contacts WHERE name = " + phone + ";";

    QSqlQuery queryNumbers(phone);
    ui->lblPhone->clear();
    ui->lblPhone->setText(phone);

    Thanks
    Attached Files Attached Files

  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: Changing text label depending on combobox selection

    QComboBox::currentText() does not return a pointer to a QString. This code will not compile.

    If phone did, somehow, contain a person's name then your attempt to build an SQL query will produce invalid SQL (except in a few unrealistic circumstances). Building SQL this way is a bad idea anyway; you should read about Approaches to Binding Values.

    If the SQL was valid, you would need to execute it, say with QSqlQuery::exec(), before you could get results. The method you have used to construct the query would execute it for you if the SQL were valid.

    If you executed it you would need to retrieve the returned value(s) , say with QSqlQuery::next() and QSqlQuery::value(), before you could set the label.
    Last edited by ChrisW67; 6th July 2012 at 05:30.

Similar Threads

  1. Depending Combobox
    By cia.michele in forum Qt Programming
    Replies: 1
    Last Post: 10th January 2012, 17:11
  2. Replies: 0
    Last Post: 23rd February 2011, 00:07
  3. Changing color of combobox placed in toolbar?
    By Jennie Bystrom in forum Qt Programming
    Replies: 0
    Last Post: 7th May 2010, 08:08
  4. changing QPushButton label
    By Ferric in forum Newbie
    Replies: 3
    Last Post: 31st January 2010, 09:57
  5. label Text is not changing using Qt Linguist
    By thomasjoy in forum Qt Tools
    Replies: 59
    Last Post: 8th October 2007, 15:24

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
  •  
Qt is a trademark of The Qt Company.