Results 1 to 2 of 2

Thread: Pass data on button click

  1. #1
    Join Date
    Aug 2013
    Location
    India
    Posts
    33
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Pass data on button click

    Hello Everyone,

    I have a widget with one LineEdit and one save button. On save button click i am calling a slot and in slot i need the data which i enter in LineEdit.
    But i am not able to get the data in slot function which i have enetered in lineedit. Thanx in advance.

    QWidget *Thresholdwindow = new QWidget;
    QLabel *label = new QLabel("Threshold Input");
    QLineEdit *lineedit = new QLineEdit(Thresholdwindow);
    QPushButton *Save = new QPushButton("Save");
    QPushButton *Cancel = new QPushButton("Cancel");
    QFormLayout *layout = new QFormLayout;
    layout->addRow(label, lineedit);
    layout->addRow(Save, Cancel);
    Thresholdwindow->setLayout(layout);
    int thresholdval = lineedit->text().toInt
    connect(Save, SIGNAL(clicked()), this, SLOT(saveThreshold(thresholdval)));

    void MainWindow::saveThreshold(int val)
    {
    qDebug() << val ;
    }

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,376
    Thanks
    246
    Thanked 687 Times in 677 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Pass data on button click

    You don't say anything about where you have defined this list of variables. If they are defined at class scope (eg. as members of the MainWindow class), then in your slot that handles the clicked() signal, you can simply access the value of "thresholdval".

    Your slot to handle the clicked() signal must have the same signature as the signal itself:

    Qt Code:
    1. MainWindow::MainWindow()
    2. {
    3. // ...
    4. connect(Save, SIGNAL(clicked()), this, SLOT(onClicked()));
    5. }
    6.  
    7. void MainWindow::onClicked()
    8. {
    9. someFunctionToSaveTheValue( thresholdval );
    10. }
    To copy to clipboard, switch view to plain text mode 

    Of course, "thresholdval" must be a member variable of the MainWindow class. If you define it as a local variable within a method, then it has no visibility outside that method.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Best way to pass data from backend c++
    By Nio74 in forum Newbie
    Replies: 10
    Last Post: 16th March 2019, 11:19
  2. on Button Click decrement the data
    By anh5kor in forum Newbie
    Replies: 2
    Last Post: 27th February 2015, 20:27
  3. Replies: 3
    Last Post: 9th September 2014, 22:55
  4. Replies: 2
    Last Post: 24th February 2014, 08:47
  5. Connect button and pass QListView current index
    By been_1990 in forum Qt Programming
    Replies: 3
    Last Post: 30th November 2009, 16:20

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.