gpt4 book ai didi

c++ - 在QMessageBox中,如何添加到Ctrl-C复制的文本中?

转载 作者:行者123 更新时间:2023-11-27 22:56:28 25 4
gpt4 key购买 nike

在 Windows 上使用 Qt、C++,Ctrl-C 从 QMessageBox 复制包含标题、消息等的文本选择。我已经添加了一些额外的字段,并希望将一些自定义文本添加到从这些字段中从标准 QMessageBox 复制的信息中。我应该在 QMessageBox 中重写什么以允许我获取已经创建的文本并向其中添加我自己的文本?

最佳答案

您需要重新实现QMessageBox 和您要使用的所有功能。这是最小的例子:

自定义消息框.h

#include <QMessageBox>
#include <QShortcut>

class CustomMessageBox : public QMessageBox
{
Q_OBJECT
public:
explicit CustomMessageBox(QWidget *parent = 0);
CustomMessageBox(Icon icon, const QString &title, const QString &text,
StandardButtons buttons = NoButton, QWidget *parent = Q_NULLPTR,
Qt::WindowFlags flags = Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
static StandardButton information(QWidget *parent, const QString &title,
const QString &text, StandardButtons buttons = Ok,
StandardButton defaultButton = NoButton);

protected:
void keyPressEvent(QKeyEvent *e);
};

自定义消息框.cpp

#include "custommessagebox.h"
#include <QClipboard>
#include <QApplication>
#include <QKeyEvent>
#include <QDialogButtonBox>

static QMessageBox::StandardButton showNewMessageBox(QWidget *parent,
QMessageBox::Icon icon,
const QString& title, const QString& text,
QMessageBox::StandardButtons buttons,
QMessageBox::StandardButton defaultButton)
{
CustomMessageBox msgBox(icon, title, text, QMessageBox::NoButton, parent);
QDialogButtonBox *buttonBox = msgBox.findChild<QDialogButtonBox*>();
Q_ASSERT(buttonBox != 0);

uint mask = QMessageBox::FirstButton;
while (mask <= QMessageBox::LastButton) {
uint sb = buttons & mask;
mask <<= 1;
if (!sb)
continue;
QPushButton *button = msgBox.addButton((QMessageBox::StandardButton)sb);
// Choose the first accept role as the default
if (msgBox.defaultButton())
continue;
if ((defaultButton == QMessageBox::NoButton && buttonBox->buttonRole((QAbstractButton*)button) == QDialogButtonBox::AcceptRole)
|| (defaultButton != QMessageBox::NoButton && sb == uint(defaultButton)))
msgBox.setDefaultButton(button);
}
if (msgBox.exec() == -1)
return QMessageBox::Cancel;
return msgBox.standardButton(msgBox.clickedButton());
}

CustomMessageBox::CustomMessageBox(QWidget *parent) : QMessageBox(parent)
{

}

CustomMessageBox::CustomMessageBox(QMessageBox::Icon icon, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QWidget *parent, Qt::WindowFlags flags)
: QMessageBox(icon, title, text, buttons, parent, flags)
{

}

void CustomMessageBox::keyPressEvent(QKeyEvent *e)
{
QMessageBox::keyPressEvent(e);

if (e == QKeySequence::Copy) {
QString separator = QString::fromLatin1("---------------------------\n");
QString tempText = QApplication::clipboard()->text();
tempText.append("Your custom text.\n" + separator);
QApplication::clipboard()->setText(tempText);
}

}

QMessageBox::StandardButton CustomMessageBox::information(QWidget *parent, const QString &title,
const QString& text, StandardButtons buttons,
StandardButton defaultButton)
{
return showNewMessageBox(parent, Information, title, text, buttons,
defaultButton);
}

用法:

void MainWindow::showCustomMessage()
{
CustomMessageBox::information(this, "New message", "A lot of text", 0);
}

此外,您可能需要覆盖 question()warning()critical() 和其他一些函数。

关于c++ - 在QMessageBox中,如何添加到Ctrl-C复制的文本中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32575840/

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