为什么聚焦时 QLineEdit 样式不改变?

发布于 2024-11-24 04:02:21 字数 337 浏览 2 评论 0原文

我正在使用 Qt 及其样式表开发 GUI。在主窗口样式表上,我放置了以下样式:

QLineEdit:focus {
    border: 2px solid #006080;
}

但是当我使用它时,样式并没有像我预期的那样真正改变。但是,如果我将相同的样式表直接放在所需的组件上,它就会像魔术一样工作!但是,将样式表放在我可能想要的每个 LineEdit 上并不是一个好主意(这会大大增加添加新组件或更改样式表所需的工作量),也不是通过添加诸如 < 之类的代码行来重新应用样式表代码>setStyleSheet(styleSheet())。

有谁知道如何解决这个问题?

I'm developing a GUI using Qt and its stylesheets. On the main window stylesheet I've put the following style:

QLineEdit:focus {
    border: 2px solid #006080;
}

But when I use it the style doesn't really change as I expected. However, if I put the same stylesheet directly on the desired component, it works like magic! But well, it's not really a good idea to put stylesheets on every single LineEdit I may want (which would greatly increase the amount of work needed to add new components or change the stylesheet), neither reapplying the stylesheet by adding code lines such as setStyleSheet(styleSheet()).

Does anyone know how to solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

筑梦 2024-12-01 04:02:48

如果需要,您可以通过编程方式设置焦点样式,如下所示:

QString styleSheet = "QLineEdit { border: 1px solid; border-color:#dcdcdc; border-radius: 4px;} QLineEdit:focus{border:1px solid gray;}";

yourFancyEdit->setStyleSheet(styleSheet);

You can set focus styles programmatically if needed like this:

QString styleSheet = "QLineEdit { border: 1px solid; border-color:#dcdcdc; border-radius: 4px;} QLineEdit:focus{border:1px solid gray;}";

yourFancyEdit->setStyleSheet(styleSheet);
少年亿悲伤 2024-12-01 04:02:46

奇怪的是,它在我的 Qt 副本上使用 QLineEdit:focus 使用

QLineEdit:focus
{
    border: 2px solid #006080;
}

你确定你没有子样式在更远的地方推翻这个吗?由于它位于主窗口上,因此它将是第一个被否决的事情。

一个潜在的解决方法是使用事件过滤器:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ui->lineEdit->installEventFilter( this );
    ui->lineEdit_2->installEventFilter( this );
}

...

bool MainWindow::eventFilter( QObject *object, QEvent *event )
{
    QLineEdit* edit = qobject_cast< QLineEdit* >( object );

    if( edit != NULL )
    {
        if( event->type( ) == QEvent::FocusIn )
        {
            edit->setStyleSheet( QString( "border: 10px solid #000000;" ) );
        }
        else if( event->type( ) == QEvent::FocusOut )
        {
            edit->setStyleSheet( QString( "border: 1px solid #000000;" ) );
        }
    }
}

当然,QStyleSheets 只是 QString,因此您可以存储预定义的样式以供使用。

Odd, it works as desired on my copy of Qt using QLineEdit:focus using

QLineEdit:focus
{
    border: 2px solid #006080;
}

Are you sure you do not have a child style somewhere farther down the line overruling this? As it is on the MainWindow it will be the first thing to be overruled.

A potential work-around is to use an event filter:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ui->lineEdit->installEventFilter( this );
    ui->lineEdit_2->installEventFilter( this );
}

...

bool MainWindow::eventFilter( QObject *object, QEvent *event )
{
    QLineEdit* edit = qobject_cast< QLineEdit* >( object );

    if( edit != NULL )
    {
        if( event->type( ) == QEvent::FocusIn )
        {
            edit->setStyleSheet( QString( "border: 10px solid #000000;" ) );
        }
        else if( event->type( ) == QEvent::FocusOut )
        {
            edit->setStyleSheet( QString( "border: 1px solid #000000;" ) );
        }
    }
}

Of course QStyleSheets are simply QStrings so you can have pre-defined styles stored away for use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文