سؤال

My Qt version is 4.7.1 and I want to set the background color of a QLineEdit the same as window color, and I use this way:

QString bgColorName = palette().color(QPalette::Normal, QPalette::Window).name();
QString strStyleSheet = QString("QLineEdit {background-color: ").append(bgColorName).append("}");
ui->lineEdit->setStyleSheet(strStyleSheet);

I tried to get the background colors name and then set the stylesheet of the QLineEdit, however, after running the application, I found the QLineEdit's color is a little different, that is, if you look at it carefully, you can see the difference, both on Win7 and Mac. Could anyone help me to find a way to get the right background color of the dialog, thank you in advance.

هل كانت مفيدة؟

المحلول

The code you posted actually works for me (using Windows 7). Have you verified with an image editor such as Photoshop that the background of the line edit isn't the same as the dialog? Sometimes the mind plays tricks and can think the background color is different (due to the border) when in fact it isn't.

By the way, you can just make the background of your QLineEdit transparent using style sheets like this:

QLineEdit le("Line Edit with transparent BG");
le.setStyleSheet("background:transparent;");
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top