Solved Problem with QTextEdit copy function after assigning it a QAction
-
I have a
QTextEdit
and aQPlainTextEdit
in the same widget. They cope with each others well with the "copy-to-clipboard" function. When I make a text selection and pressed Ctrl+C, the corresponded TextEdit would do the copying.But then I'm having trouble trying to add a button for the command. I created a
QAction * m_pActionCopy
and connect it to a slot// m_textEditor created from QPlainTextEdit // m_lineText created from QTextEdit void MainWindow::slotCopy() { m_textEditor->copy(); }
Now the
QPlainTextEdit
is taking control of the copy function. When I highlight text and copy from theQTextEdit
, the program was still listening to theQPlainTextEdit
widget. How do I fix this? -
Hi,
What type of button ?
How are you creating your action ? -
The button is a push button. I also added the action into a menu.
QAction * m_pActionCopy = new QAction(icon, "Copy", this); m_pActionCopy->setShortcuts(QKeySequence::Copy); m_pActionCopy->setCheckable(false); connect(m_pActionCopy, SIGNAL(triggered()), this, SIGNAL(slotCopy())); pEditMenu->addAction(m_pActionCopy);
-
@lansing
I'm afraid I do not understand what you are saying at all. But your slot hasm_textEditor->copy();
, so it will always copy from yourQPlainTextEdit
. If you want something to copy fromQTextEdit
, you will need a a slot containingm_lineText->copy();
. -
@JonB said in Problem with QTextEdit copy function after assigning it a QAction:
If you want something to copy from QTextEdit, you will need a a slot containing m_lineText->copy();.
ah, now I maybe understand what @lansing want to do.
void MainWindow::slotCopy() { if( focusWidget() == m_textEditor) m_textEditor->copy(); else if( focusWidget() == m_lineEdit) m_lineEdit->copy(); }
-
Yes, that's what I want. Thanks. I tested it and it works.