I am writing a programm, that creates a QLineEdit that only accepts digits and should, when it rejects an input that is not a digit, turn the background to some abitrary color. If the input is accpeted it will turn the background white again. Now I need to connect the inputRejected and textEdited events of the QLineEdit to randomcolor() and white() respectively, but the connect is making me trouble and i do not know how to fix it.
This is the first time I'm working with the connect and i slugged through many forums alreay trying out the different Syntax i found there.
#include <QtWidgets>
class OnlyNumbers : QLineEdit {
public:
static int spawn(int argc, char *argv[]){
QApplication app(argc, argv);
OnlyNumbers P;
return app.exec();
}
OnlyNumbers() : QLineEdit() {
this->setValidator(new QIntValidator());
QObject::connect(this, SIGNAL(inputRejected()), this, SLOT(randomcolor()));
QObject::connect(this, SIGNAL(&QLineEdit::textEdited(const QString)), this, SLOT(&OnlyNumbers::white()));
QRegExp rx("[0-9]*"); QValidator *validator = new QRegExpValidator(rx, this);
this->setValidator(validator);
this->show();
}
public slots:
void randomcolor(){
this->setStyleSheet("QLineEdit { background: rgb(std::rand()%256, rand()%256, rand()%256); selection-background-color: rgb(rand()%256, rand()%256, rand()%256); }");
}
void white(){
this->setStyleSheet("QLineEdit { background: rgb(255, 255, 255); selection-background-color: rgb(233, 99, 0); }");
}
};
int main(int argc, char *argv[])
{
return OnlyNumbers::spawn(argc, argv);
}
QObject::connect: No such slot QLineEdit::randomcolor()
QObject::connect: No such signal QLineEdit::&QLineEdit::textEdited(const QString)
These are the errors i get, and i don't know what to do with them, because for em the two are existant. Sadly I cannot describe my problem better, because i don't know more.
SOLVED: The Problem was, that i didn't seperate calls definition and declaration in onlynumbers.h and onlynumbers.cpp. Also i cannot put std::rand()%256 in the string i need to split the string and concatonate it with all the numbers converted to a qstring. :D Thanks for the help. You gave me the motivation to keep googeling.