Important: Please read the Qt Code of Conduct -

Stylesheet in QHBoxLayout embedded in QFormLayout

  • Hi,
    I have the following code:

    #include "additem.h"
    #include "ui_additem.h"
    Additem::Additem(QWidget *parent) :
        ui(new Ui::Additem)
    	Addcontent ();
        delete ui;
    void Additem::Addcontent()
    //Add Title
        QLabel  *Title	= new QLabel;
        QFont f("Arial", 18, QFont::Bold);
        Title->setFont (f);
        Title->setAlignment (Qt::AlignCenter);
        Title->setText ("ADDING A FRIEND TO THE DATABASE");
        Title->setFixedWidth (840);
        Title->show ();
    //Add ID Counter
        Additem::opendb ();
        QSqlQuery query("SELECT ID FROM Items ORDER BY ID DESC");
                qDebug() << "The query is active.";
                qDebug() << "The query is NOT active.";
        query.first ();
        int LastID;
        LastID = query.value(0).toInt ();
        qDebug() << "The last ID is " << LastID;
        int ItemID = LastID+1;
        qDebug() << "The Item ID # is " << ItemID;
        QString sID = QString::number(ItemID);
        QLabel *Label_ID = new QLabel;
        QFont g("Arial", 16, QFont::Normal);
        Label_ID->setFont (g);
        QLabel *ID_Display = new QLabel;
        ID_Display->setFont (g);
        Label_ID ->setText ("Friend ID:");
        ID_Display ->setText (sID);
        db.close ();
    //Getting name
        QLabel *Label_Name = new QLabel;
        Label_Name->setFont (g);
        Label_Name->setText ("Name:");
        QLineEdit *LineEdit_Name = new QLineEdit;
        LineEdit_Name->setFont (g);
        QString string;
        string = LineEdit_Name->text ();
        setFocusPolicy (Qt::StrongFocus);
        connect(LineEdit_Name, SIGNAL(editingFinished()), this, SLOT(readAndValidate()));
        setStyleSheet("QLineEdit{ "
                      "background-color:rgb(202, 255, 227);"
                      "border: 2px solid gray;"
                      "border-radius: 10px;"
                      "padding: 0 8px;"
                      "selection-background-color: darkgray;"
                      "font-size: 16px;}"
                      "QLineEdit:focus { "
                      "background-color:rgb(192, 192, 255);}"
        LineEdit_Name->setFixedWidth (150);
        LineEdit_Name->setFixedHeight (35);
        QLabel *angry_image_Label = new QLabel;
        angry_image_Label->setFixedSize (30,30);
    //    QLabel *incorrect_Label = new QLabel();
    //    incorrect_Label->setStyleSheet("QLabel { font-size:200px; color: red; }");
        QLabel *incorrect_Label = new QLabel();
        incorrect_Label->setStyleSheet("QLabel { font-size:200px; color: red; }");
                     if(string.length ()<2)
                           incorrect_Label->setText ("The name is too short!");
                           incorrect_Label->setStyleSheet ("color: red");
                           QPixmap pix("C:/Programming/Projects/FolkFriends/icons/angry.png");
                           angry_image_Label->setScaledContents (true);
                           angry_image_Label->setPixmap (pix);
                           incorrect_Label->setText ("");
                           QPixmap pix2("C:/Programming/Projects/FolkFriends/icons/Check-icon.png");
                           angry_image_Label->setScaledContents (true);
                           angry_image_Label->setPixmap (pix2);
                   QString Name;
                   Name = LineEdit_Name->text ();
                   qDebug() << "Name: " << Name;
    //Creating the scroll area
        	QScrollArea *scrollarea = new QScrollArea(this);
            scrollarea->setVerticalScrollBarPolicy (Qt::ScrollBarAlwaysOn);
            scrollarea->setHorizontalScrollBarPolicy (Qt::ScrollBarAlwaysOn);
            scrollarea->setMinimumWidth (900);
            QWidget *viewport  = new QWidget(this);
            scrollarea->setWidget (viewport);
            scrollarea->setWidgetResizable (true);
            QFormLayout *layout = new QFormLayout(this);
            viewport->setLayout (layout);
    		layout->setSpacing (60);
            //Title Row setup
            QHBoxLayout *titleRow = new QHBoxLayout();
            titleRow->addWidget (Title);
    		//Creating the Name row
    		QHBoxLayout *NameRow = new QHBoxLayout();
            NameRow->addWidget (Label_Name);        
            NameRow->addWidget (LineEdit_Name);
            NameRow->addSpacing (10);
            NameRow->addWidget (angry_image_Label);
            NameRow->addSpacing (10);
            NameRow->addWidget (incorrect_Label);
    		NameRow->addSpacing (350);
            layout->setVerticalSpacing (30);
            QLabel *spacer_Label = new QLabel;
            //Displaying the rows
    		layout->addRow (titleRow);
            layout->addRow (spacer_Label,spacer_Label);
            layout->addRow (Label_ID,ID_Display);
            layout->addRow (NameRow);

    The incorrect_Label's formatting does not show when displayed in the layout. It shows when I display it outside of the layout. The color is correct even in the layout. What can I do to increase the text size of incorrect_Label?
    Thank you for your help.

  • Lifetime Qt Champion


    You are applying several different style sheets on your widget for QLineEdit. Either remove the QLineEdit specifier on incorrect_Label or use an object name to differentiate it.

  • Thank you!