QLineEdit焦点事件

发布于 2024-09-26 18:06:06 字数 997 浏览 0 评论 0原文

我有两个小部件 mainwindow123 和二等。在我的 MainWidget.cpp 中有一个行编辑和按钮字段。最初我可以将焦点设置在行编辑上。但在来自第二个.cpp 小部件之后,我无法将焦点设置在 lineedit 上。请帮助我..我在哪个地方做错了?提前致谢。

这是我的代码 MainWidget.cpp

MainWidget::MainWidget(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::MainWidget)
    {
        ui->setupUi(this);
        s = new second();
        connect(ui->pushButton, SIGNAL(clicked()),this,SLOT(callSecond()));

    }

    MainWidget::~MainWidget()
    {
        delete ui;
    }
    void MainWidget::callSecond()
    {
       s->show();

     }

第二个.cpp

second::second(QWidget *parent) :
    QWidget(parent)
{
    QPushButton *first = new QPushButton("first");
    first->setStyleSheet(
         "background-color:black;"

    );
    QGridLayout *d = new QGridLayout();

    d->addWidget(frist,0,0,1,1);
    setLayout(d);
    connect(first,SIGNAL(clicked()),this,SLOT(first()));
}

void second:: first()
{
    this->hide();
}

I have two widget mainwindow123 and second-class. In my MainWidget.cpp have one lineedit and button field. Initially I can set the focus on the line edit. But after come from the second.cpp Widget then I could not set the focus on the lineedit. Please help me.. Which place I did the mistake? Thanks in advance.

This is my code
MainWidget.cpp

MainWidget::MainWidget(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::MainWidget)
    {
        ui->setupUi(this);
        s = new second();
        connect(ui->pushButton, SIGNAL(clicked()),this,SLOT(callSecond()));

    }

    MainWidget::~MainWidget()
    {
        delete ui;
    }
    void MainWidget::callSecond()
    {
       s->show();

     }

second.cpp

second::second(QWidget *parent) :
    QWidget(parent)
{
    QPushButton *first = new QPushButton("first");
    first->setStyleSheet(
         "background-color:black;"

    );
    QGridLayout *d = new QGridLayout();

    d->addWidget(frist,0,0,1,1);
    setLayout(d);
    connect(first,SIGNAL(clicked()),this,SLOT(first()));
}

void second:: first()
{
    this->hide();
}

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

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

发布评论

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

评论(1

司马昭之心 2024-10-03 18:06:06

这是因为单击按钮后您的焦点会转到按钮上。您可以通过以下方式实现:

  1. 设置 focusProxy http://doc.qt.io/ qt-4.8/qwidget.html#setFocusProxy
  2. 禁用按钮的强焦点: http://doc.qt.io/qt-4.8/qwidget.html#focusPolicy-prop
  3. 将按钮单击信号连接到 QLineEdit 的 setFocus 插槽

It's because your focus goes to button after you clicked it. You could achieve it by:

  1. Setting a focusProxy http://doc.qt.io/qt-4.8/qwidget.html#setFocusProxy
  2. Disabling strong focus on button: http://doc.qt.io/qt-4.8/qwidget.html#focusPolicy-prop
  3. Connecting buttons clicked signal to setFocus slot of your QLineEdit
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文