How to disable shortcuts in QTextEdit - qt

I'm working in a scientific calculator project using Qt5, I'm using the QTextEdit as the calculator's display.
I want to disable the shortcuts like (Ctrl + A, and Ctrl + C) in the display, so How can I do that?
Thank you.

Key Filter Method, Create an Event Filter that returns false for the Hot Keys. It's a little tedious, but should work out.

Event filtering on the LineEdit is the proper way to do it, then you can ignore the ones you do not want or override the behavior.
A dirty shortcut (no pun intended) to try is to create a QShortcut and and assign it to an empty slot. Qt will probably complain about ambiguous shortcuts and will probably do not do anything with it.
Dirty I know :)
QShortcut *shortcut = new QShortcut(QKeySequence("Ctrl+A"), parent);
QObject::connect(shortcut, SIGNAL(activated()), receiver, SLOT(emptySlot()));
May be you can even ignore the connect part...

Related

Making changes to a QTextEdit without adding an undo command to the undo stack

I'm looking for a way to change the QTextCharFormat of a QTextEdit's QTextBlock without triggering the addition of an undo command. Let me explain:
The QTextCharFormat of a QTextBlock can be easily changed by using the QTextCursor::setBlockCharFormat() method. Assuming we have a QTextEdit called myTextEdit whose visible cursor is within the text block we want to change, we can change the textblock's QTextCharFormat like so:
text_cursor = myTextEdit.textCursor()
text_cursor.setBlockCharFormat(someNewCharFormat)
The above code works fine, but it will also add an undo command to the myTextEdit undo stack. For my own purposes, I would like to be able to change the QTextCharFormat of a QTextBlock without adding an undo command to the QTextEdit's undo stack.
I considered temporarily disabling the undo/redo system with the QTextDocument::setUndoRedoEnabled() method, but that method also clears the undo stack, which I don't want to do. I've also looked for other ways to change how the undo/redo system behaves, but I haven't found a way to get it to temporarily ignore changes. I simply want to make a change to a QTextEdit without the undo/redo system registering the change at all.
Any tips or suggestions are appreciated. Thanks!
You have to group this with previous modification. It is simple you have to surround code which does this modification with: beginEditBlock and endEditBlock. See documentation.
text_cursor = myTextEdit.textCursor()
text_cursor.beginEditBlock()
text_cursor.setCharFormat(someOtherCharFormat) # some previous modification
text_cursor.setBlockCharFormat(someNewCharFormat)
text_cursor.endEditBlock()
this way you will make a single commit for undo stack for any complex modification.
joinPreviousBlock() should do the trick:
cursor = self.textCursor()
cursor.joinPreviousEditBlock()
cursor.setPosition(start, QTextCursor.MoveAnchor)
cursor.setPosition(end, QTextCursor.KeepAnchor)
cursor.setCharFormat(fmt)
cursor.endEditBlock()
You should use QSyntaxHighlighter. Extends it and implement highlightBlock func, and call setFormat in it to change format without making undo/redo stack. See documentation for more detail.
If you feel QSyntaxHighlighter is not what you want, you can use QTextLayout. It is low level api and its setAdditionalFormats func doesn't make any undo stack.
range1 = QTextLayout.FormatRange()
range1.start = 0
range1.length = 10
range1.format = QTextCharFormat()
# additional ranges here...
textBlock.layout().setAdditionalFormats([range1, ...])
This is also used in the inside of QSyntaxHighlighter.

QFileDialog component signals

I am subclassing QFileDialog to try to get some custom behavior. I would like to connect to signals emitted by components of the dialog, e.g. the textEdited signal when the file name line edit is manually edited. I understand that QFileDialog emits some signals itself, but these do not cover the cases I would like to respond to.
I have two ways about this I can think of, but don't know how to implement. One is to somehow attain a reference to the component to connect to it's signal. The other would be something with event filters, but the event source is the dialog itself, so I don't know how to determine where mouse clicks or key presses occur.
Are either of these methods feasible? Or another way?
Here is one option (your first suggestion):
dialog = QFileDialog()
layout = dialog.layout()
# for i in range(layout.rowCount()):
# for j in range(layout.columnCount()):
# try:
# print i,j
# print layout.itemAtPosition(i,j).widget()
# except:
# pass
line_edit = layout.itemAtPosition(2,1).widget()
line_edit.setText('Hello Stack Overflow')
dialog.exec_()
This gives you access to the QLineEdit in the dialog, which has a bunch of signals you can connect to.
I've also included the code I used to find this widget. I just iterated over the widgets in the layout of the dialog and found the indices of the one I was after. So if you need access to anything else in the dialog, you should be able to find it pretty easily!
Downside to this method: If the layout changes in a future version of Qt, this will break. I suppose you could make the algorithm more robust by looking for widgets that are instances of QLineEdit, but there are always risks with hacky approaches like this!

Qt - signal for when QListWidget row is edited?

I am working in Qt4.7, and I have a QListWidget in my dialog. I have a QString that needs to match the current text in the row of this widget (the individual rows are editable). Looking at the signals associated with QListWidget, there seem to be signals for when a different index is selected but none for when the text of a the currently selected row changes. I thought currentTextChanged(QString) would do it, but it didn't. I also thought to try to connect each individual row to something, but QListWidgetItem doesn't have any built-in signals. Does anyone know of a way to do this? Thanks!
At first it seems like QListWidget::itemChanged is the way to go, but soon you run into a problem: the signal is sent for everything - inserts, changing colors, checking boxes, and anything else that "changes" the item! Predelnik pointed that out in his answer. Some people have tried to put in flags and filter everywhere by intercepting various signals to find out if editing was the actual event. It gets very messy.
There is also QAbstractItemModel::dataChanged , which would seem like a good solution. It even has a parameter "const QVector& lstRoles" so you could scan for Qt::EditRole and see if it was really edited. Alas, there's a catch - it gets called for everything just like QListWidget::itemChanged and unfortunately, for QListWidget anyway, the roles parameter is always empty when it's called (I tried it). So much for that idea...
Fortunately, there's still hope... This solution does the trick! :
http://falsinsoft.blogspot.com/2013/11/qlistwidget-and-item-edit-event.html
He uses QAbstractItemDelegate::closeEditor, but I prefer using QAbstractItemDelegate::commitData.
So make a connect like so...
connect(ui.pLstItems->itemDelegate(), &QAbstractItemDelegate::commitData, this, &MyWidget::OnLstItemsCommitData);
Then implement the slot like this...
void MyWidget::OnLstItemsCommitData(QWidget* pLineEdit)
{
QString strNewText = reinterpret_cast<QLineEdit*>(pLineEdit)->text();
int nRow = ui.pLstItems->currentRow();
// do whatever you need here....
}
Now you have a slot that gets called only when the list item's text has been edited!
I guess you need to look into the following signal:
void QListWidget::itemChanged(QListWidgetItem * item)
But be careful because it's being sent every time some property of item changed, not only text. I remember when we ran into the problem once when we changed item colors and got tons of false positive slots called because of that. If you need more fine tuning I guess it's better to write model/view classes yourself and not rely on QListWidget.

How to connect multiple QShortcuts to one slot in Qt? And find out inside the slot which shortcut emitted it?

Is it possible to pass parameters?
Something like this:
shortcut_key_one, channelSLot(int)
shortcut_key_two, channelSLot(int)
shortcut_key_three, channelSLot(int)
If it doesn't matter what the shortcut was, assign the shortcuts to a QAction using QAction::setShortcuts.
font_increase_action_ = new QAction(tr("&Increase font sizes"), this);
font_increase_action_->setShortcuts(QList<QKeySequence>()
<< Qt::CTRL + Qt::Key_BracketRight
<< Qt::CTRL + Qt::Key_Greater);
connect(font_increase_action_, SIGNAL(triggered()), SLOT(IncreaseFontSizes()));
If you really need to know which shortcut was pressed, you could assign each one to a separate QAction and then use the QSignalMapper.
Well, to do exactly what you're asking, you could keep track of all of your QShortcut objects and then use the sender() function inside of your slot to determine which QShortcut caused the slot to be triggered.
However, it sounds like you seriously want to consider using and overriding QWidget::keyPressEvent() instead if at all possible. It would be a lot easier to ask the QKeyEvent object which key was pressed than to do all this crazy QShortcut mess.

Qt SLOTS parameters (Beginner)

I'm a beginner to Qt and am making (or at least trying to make) a basic calculator. If I understand correctly, when doing this:
connect(my_button_4, SIGNAL(clicked()), this, SLOT(writeNumberLbl("4")));
The "4" is not accessible (rather, only its type is) in writeNumberLbl. Basically, I would like so that when the button is clicked, the label sets its text to "4". However, I have the numbers 0 to 9, so I wanted to do:
connect(my_button_0, SIGNAL(clicked()), this, SLOT(writeNumberLbl("0")));
connect(my_button_1, SIGNAL(clicked()), this, SLOT(writeNumberLbl("1")));
...
connect(my_button_9, SIGNAL(clicked()), this, SLOT(writeNumberLbl("9")));
My writeNumberLbl function is:
void Calculator::preWriteVal(QChar val)
{
QString curVal = ui.lbl_output->text();
curVal += val;
ui.lbl_output->setText(curVal);
}
However, I can see that this will not work due to the parameter, 'val'. Could someone please point me in the right direction? Thank you. I did look to see if this question had already been answered and couldn't find anything. If it has, please provide me a link.
Also, is it possible, using Qt Designer 4, to connect a widget to a custom slot?
Thank you.
As far as I know,Qt's signal/slot system requires that the number of parameters of signal function is not less than that of slots function.
In your example,the signal function click() have zero parameters and slot function writeNumberLbl("4") have one parameter,this will not be accepted by the Qt's signal/slot system and if you run your program in debug mode of Qt creator,the qDebug will show you a message like 'Incompatiable signal/slot' blalbalba~. To solve this problem, just read the article given by Arnold Spence. It is quite clear.
There are a number of ways to tackle this problem and they are outlined very nicely here. Although that page is a bit old, I think it is still quite valid. I would recommend using a signal mapper.
For your second question, yes. You can connect signals and slots using Qt Designer by setting the designer in "Edit Signals/Slots" mode. Once in this mode, for example, you can drag a connection line from a button to the form. A dialog will open up allowing you to choose the signal and slot to connect. If you haven't already implemented a slot in code, you can specify the name of a slot and then add the code for it afterward.
The number of parameters in Slot can not exit those in Signal? and pressed() has none. You have two choices (three, counting the dumb one):
Use QSignalMapper. Its help is self-explanatory.
Connect all your buttons to single slot. In it, find out what button has been pressed. QObject::sender() function helps.
There are even more ways, but more complicated.

Resources