gpt4 book ai didi

c++ - 通过 eventFilter 获取 QLineEdit 的某些属性到剪贴板

转载 作者:行者123 更新时间:2023-11-30 05:13:43 25 4
gpt4 key购买 nike

我正在尝试创建一个 QLineEdit 元素,其文本将在单击时自动复制到剪贴板。

我创建了以下 eventFilter 来捕获点击事件并将其安装在适用的元素上:

bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
if(event->type() == QEvent::MouseButtonPress)
{
qDebug("TEST");
return true;
}
else
{
return false;
}
}

从这里收集我需要的数据并传递给剪贴板函数的最佳方法是什么?

最佳答案

使用 QClipboard类(class)。您可以使用 qApp->clipboard() 获取应用程序的剪贴板然后从 QLineEdit 设置文本:

bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
if(event->type() == QEvent::MouseButtonPress)
{
auto watched_as_lineEdit = qobject_cast<QLineEdit*>(watched);
if (watched_as_lineEdit != nullptr) {
qApp->clipboard()->setText(watched_as_lineEdit->text());
return true;
}
}

return QMainWindow::eventFilter(watched, event); // change for actual parent class if different from QMainWindow
}

关于c++ - 通过 eventFilter 获取 QLineEdit 的某些属性到剪贴板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43806866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com