Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

using QRegExp and Validator in Stylesheet?



  • In my header file i am using like this for all my lineEdit objects

                 #define length 6
                  #define display "XxYy00"
                  #define format"^[A-Z]{1,1}[a-z]{1,1}[A-Z]}$"
    		QRegExp idFormat(TEST_ID_FORMAT);
    		QRegExpValidator *testIDValidator = new QRegExpValidator(idFormat);
    		
    
    		test0->setValidator(testIDValidator);
    		test0->setText(display );
    		test0->setMaxLength(length );
    		test0->setAlignment(Qt::AlignCenter);
    		test0->setFont(textFont);
    

    However instead of doing this stuff in header file i would like to try this to implement via StyleSheets. I think this can be possible in stylesheet and i tried like this

    In my lineEdit i did change StyleSheet and added like this

    QLineEdit:
    {
    	TEST_ID_FORMAT "^[A-Z]{1,1}[a-z]{1,1}[A-Z]{1,1}[a-z]{1,1}[0-9]{1,1}[0-9]{1,1}$";
            QRegExp idFormat(TEST_ID_FORMAT);
            QRegExpValidator *testIDValidator = new QRegExpValidator(idFormat);
    }
    

    It seems to be i am doing some wrong but i am unable to find it till now :-(. All this setMaxLenght are not implemented in this stylesheet but i will be doing it later if it works :-)


  • Lifetime Qt Champion

    Hi,

    Do I understand correctly that you are trying to instanciate C++ object in a style sheet ?



  • @SGaist No, But what i am trying to say was like normally till now for example in my lineEdit object i always restrict to enter only data which is like ip address(P.S the code showed in the question was bit different but idea is still same) by using validator and RegExp. And I am asking if i can do the same using stylesheets? I mean i want this information to be in stylesheets which will be embedded in ui file.

    What i mean by doing using stylesheets means can i use validator and Regexp in stylesheets and remove that code part in my header file?

    I would like to know if there is such method. I did tried to search in qt forum and also in stack but from what i found was there is no such way or may be even i understood wrong by reading few things.



  • @aravmadd
    Style sheets (CSS, QSS) are for visual style. You can't put regular expressions or validators or similar into style.



  • @JonB Thanks for the clarity information. I was thinking the same .


Log in to reply