代码之家  ›  专栏  ›  技术社区  ›  Narek

QT-如何在QLineEdit上应用QToolTip

  •  1
  • Narek  · 技术社区  · 15 年前

    在一个对话框中,我有一个QLineEdit和一个按钮。当我按下按钮时,我想为QLineEdit启用一个工具提示(在它里面或下面)。请给我一个代码片段。

    2 回复  |  直到 15 年前
        1
  •  7
  •   mtvec    15 年前

    下面是一个简单的例子:

    class MyWidget : public QWidget
    {
            Q_OBJECT
    
        public:
    
            MyWidget(QWidget* parent = 0) : QWidget(parent)
            {
                QVBoxLayout* layout = new QVBoxLayout(this);
                edit = new QLineEdit(this);
                layout->addWidget(edit);
                showButton = new QPushButton("Show tool tip", this);
                layout->addWidget(showButton);
                hideButton = new QPushButton("Hide tool tip", this);
                layout->addWidget(hideButton);
    
                connect(showButton, SIGNAL(clicked(bool)), this, SLOT(showToolTip()));
                connect(hideButton, SIGNAL(clicked(bool)), this, SLOT(hideToolTip()));
            }
    
        public slots:
    
            void showToolTip()
            {
                QToolTip::showText(edit->mapToGlobal(QPoint()), "A tool tip");
            }
    
            void hideToolTip()
            {
                QToolTip::hideText();
            }
    
        private:
    
            QLineEdit* edit;
            QPushButton* showButton;
            QPushButton* hideButton;
    };
    

    正如您所看到的,没有简单的方法来启用某个小部件的工具提示。你必须提供全局坐标 QToolTip::showText .

    QHelpEvent 并使用 QCoreApplication::postEvent QWidget::setToolTip . 不过,你还是要提供全球坐标。

    我真的很感兴趣,为什么你要这样做,因为工具提示是打算显示只有当你的鼠标悬停 当你问“这是什么”的时候。把它用在别的地方看起来是个糟糕的设计。如果你想给用户一个信息,为什么不使用 QMessageBox

        2
  •  5
  •   mosg    15 年前

    如果您需要QLineEdit的工具提示,那么问题是什么?刚刚设定:

    myLineEdit->setToolTip("Here is my tool tip");
    

    但如果你需要在一段时间后再看一些文字 button 按下,这里有另一个解决方案:例如,创建一个槽 on_myBytton_clicked() 把它接到你的按钮上。在插槽中执行 QLabel QTextEdit

    推荐文章