minecraftplayer1234
minecraftplayer1234

Reputation: 2227

Connecting a button in Qt

Okay. So I've got simple Qt app, which has lineEdit and pushButton QObjects. Pressing the button should print the text which is in lineEdit to a .txt file. Simple. I've run this code, when function didn't take any parameters (just wrote some text to a file). Now it looks like this:

mainwindow.h

private slots:
    static void on_some_pushButton_clicked(QString s);

mainwindow.cpp

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent),
    ui (new Ui::MainWindowClass)
{
    ui->setupUi(this);
    ui->pushButton->setText("GO");
    //Connecting a GO pushButton to a func which saves your name into a .txt file
    auto s = ui->lineEdit->text();
    connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(on_some_pushButton_clicked(s)));
}

void MainWindow::on_some_pushButton_clicked(QString s)
{
    FILE *file;
    file=fopen("data.txt", "wt");
    fprintf(file, "XD\n");
    auto z=(s.toStdString()).c_str();
    fprintf(file, "%s", z);
    fclose(file);
}

main.cpp (just in case)

#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.setWindowTitle("Qt App");
    w.show();
    return a.exec();
}

And, well, it doesn't do anything. I don't see the file being created, or anything like that. What am I doing wrong?

P.S When I said that it works without parameters, I meant:

mainwindow.cpp

void MainWindow::button_clicked()
{
    FILE *file;
    file=fopen("data2.txt", "wt");
    fprintf(file, "Works with no args");
    fclose(file);
}

and:

connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(button_clicked()));

Upvotes: 3

Views: 15413

Answers (1)

Yuushi
Yuushi

Reputation: 26040

This connect format:

connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(on_some_pushButton_clicked(s)));

is wrong. Within SIGNAL() and SLOT(), you are supposed to supply function declarations, that is, it should contain types, not variables. So this should instead look like:

connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(on_some_pushButton_clicked(QString)));

You're trying to bind a variable (s) that is to be passed through instead. To do what you look like you want to do, you'll need to have an instance variable (or have a closure, or chain signals together...but the instance variable way is easier to explain).

class MainWindow
    : public QMainWindow
{
    // Stuff as before

private:
    QString s;
};

Then, in your constructor:

s = ui->lineEdit->text();

Of course, this isn't passing through a QString at all now (it's just reading a member variable), the function doesn't need it as a parameter; the connect should go back to looking like:

connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(on_some_pushButton_clicked()));

You should probably post the output from your App as well, as Qt is likely saying something along the lines of No such slot when you click your button.

Upvotes: 6

Related Questions