No matching function?
-
Hi, I'm trying to create a program and I'm currently working on a function that turns a checkbox off if the line-edit next to it is empty but it toggles it on when it's not empty. I have this weird error :
@C:\Users\Robbe\Desktop\C++\workspace\mta\mainwindow.cpp:30: error: no matching function for call to 'MainWindow::toggleCheckBox(QCheckBox*&, QLineEdit*&)'
MainWindow::toggleCheckBox(ui->checkBoxCreateServerFile,ui->lineEditCreateServerFile);
^@@#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QCheckBox"
#include "QString"
#include "QLineEdit"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}void MainWindow::toggleCheckBox(QCheckBox checkBox,QLineEdit lineEdit){
QString text = lineEdit.text();
if(text == ""){
checkBox.setChecked(false);
}else{
checkBox.setChecked(true);
}
}MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::on_lineEditCreateServerFile_textEdited()
{
MainWindow::toggleCheckBox(ui->checkBoxCreateServerFile,ui->lineEditCreateServerFile);
}
void MainWindow::on_lineEditCreateClientFile_textEdited()
{
MainWindow::toggleCheckBox(ui->checkBoxCreateClientFile,ui->lineEditCreateClientFile);
}
@Here's my header :
@#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include "QCheckBox"
#include "QLineEdit"namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
void toggleCheckBox(QCheckBox checkBox,QLineEdit lineEdit);
explicit MainWindow(QWidget *parent = 0);
~MainWindow();private slots:
void on_lineEditCreateServerFile_textEdited();void on_lineEditCreateClientFile_textEdited();
private:
Ui::MainWindow *ui;
};#endif // MAINWINDOW_H
@Also is there a way to make this easier? Because I have like 10 line-edits I have to call this function on? I'm talking about creating some kind of general event checker because I'm not really interested in writing all of those slots like this @void MainWindow::on_lineEditCreateClientFile_textEdited()
{
MainWindow::toggleCheckBox(ui->checkBoxCreateClientFile,ui->lineEditCreateClientFile);
}@ -
Hi,
@void toggleCheckBox(QCheckBox checkBox,QLineEdit lineEdit);@
Take objects and your giving it pointers. Note that you can't copy QObject and QObject derived classes.
Update the signature to that
@void toggleCheckBox(QCheckBox *checkBox,QLineEdit *lineEdit);@
And reimplement the function accordingly.
Also, you call it like a static function which it is not.
Hope it helps
-
[quote author="SGaist" date="1397249905"]Hi,
@void toggleCheckBox(QCheckBox checkBox,QLineEdit lineEdit);@
Take objects and your giving it pointers. Note that you can't copy QObject and QObject derived classes.
Update the signature to that
@void toggleCheckBox(QCheckBox *checkBox,QLineEdit *lineEdit);@
And reimplement the function accordingly.
Also, you call it like a static function which it is not.
Hope it helps[/quote]
Thank you.
-
Hi, if you have many of those check box and line edit combination you could create a custom QWidget and reuse that maybe, I don't know how well that works with the designer (maybe not that good).
If you want to keep it simple you could at least add a generic function that takes a QCheckBox and QLineEdit and do your "property binding" in there, so you can call this function for all your combination but only have to define the code once at least.example:
@
void MainWindow::connectCheckBoxToLineEdit(QCheckBox *cb, QLineEdit *le)
{
connect(le, &QLineEdit::textChanged, [cb](const QString &text) {
cb->setEnabled(!text.isEmpty());
});
}
@
you only have to define that function once, in that example I have connected the textChanged signal of the line edit to the check box, so every time the text changes the expression is evaluated and the check box is only enabled if the line edit contains text.
you can use that function like this:
@
connectCheckBoxToLineEdit(ui->checkBox, ui->lineEdit);
connectCheckBoxToLineEdit(ui->checkBox2, ui->lineEdit2);
...
@
I've used an anonymous slot with a c++ lambda function there, hope that helps and doesn't confuse you :) -
[quote author="Xander84" date="1397251136"]Hi, if you have many of those check box and line edit combination you could create a custom QWidget and reuse that maybe, I don't know how well that works with the designer (maybe not that good).
If you want to keep it simple you could at least add a generic function that takes a QCheckBox and QLineEdit and do your "property binding" in there, so you can call this function for all your combination but only have to define the code once at least.example:
@
void MainWindow::connectCheckBoxToLineEdit(QCheckBox *cb, QLineEdit *le)
{
connect(le, &QLineEdit::textChanged, [cb](const QString &text) {
cb->setEnabled(!text.isEmpty());
});
}
@
you only have to define that function once, in that example I have connected the textChanged signal of the line edit to the check box, so every time the text changes the expression is evaluated and the check box is only enabled if the line edit contains text.
you can use that function like this:
@
connectCheckBoxToLineEdit(ui->checkBox, ui->lineEdit);
connectCheckBoxToLineEdit(ui->checkBox2, ui->lineEdit2);
...
@
I've used an anonymous slot with a c++ lambda function there, hope that helps and doesn't confuse you :)[/quote]Mhhh.. Seems a better way to do it indeed.