Overwrite Copy Text operation from a QTreeWidget - c++

I have a QTreeWidget where I would to overwrite the Copy Text (ctrl+c) from the QTreeWidgetItem.
The default behaviour is to copy the text from the selected column, but I'd like to update the values with more information that I have on the background.
Is there a native way of doing it without capturing the ctrl+c with a keyPressEvent ?

You can try the following approach to store your own stuff in clipboard without overriding QWidget::keyPressEvent():
// Assuming tree widget already exists.
auto shortcut = new QShortcut(QKeySequence("Ctrl+C"), treeWidget);
QObject::connect(shortcut, &QShortcut::activated, [treeWidget] () {
auto selected = treeWidget->selectedItems();
// Get only first selected item's text.
if (selected.size() > 0)
{
QClipboard *clipboard = QApplication::clipboard();
clipboard->setText(QString("Custom clipboard text: %1").arg(selected.at(0)->text(0)));
}
});
In the code I used QShortcut to handle Ctrl+C key sequence and store selected tree widget item's text (customized) into the clipboard.

Related

Close button only on active tab of QTabWidget

To save space in a QTabWidget, I would like to show the close icon only for the current tab, like e.g. Firefox is doing:
Is there a simple way using a style sheet, some thing like (not working like this)
QTabBar::tab::!selected::close-button {visible: false;}
or do I have to subclass QTabWidget to get the desired behavior?
You won't need to subclass anything, you can use QTabWidget::tabBar() method to obtain a reference (i.e. QTabBar *) to the tab bar associated with your QTabWidget. (Note that this method is no longer protected, so it can be accessed without subclassing the class)
QTabBar *tabBar = tabWidget->tabBar();
You can now use tabBar reference to hide close buttons on non-current tabs. For example to hide ith button, you can do:
tabBar->tabButton(i, QTabBar::RightSide)->hide();
So a simple workflow could be as follows:
Connect QTabWidget::currentChanged(int index) signal to a slot.
In that slot hide all close buttons other than the button at index.
You can subclass QTabWidget to get access to the QTabBar widget using protected method QTabWidget::tabBar. Then you can connect to the QTabBar::currentChanged signal and hide close button for not selected tabs manually:
QTabBar::ButtonPosition closeSide =
(QTabBar::ButtonPosition)style()->styleHint(QStyle::SH_TabBar_CloseButtonPosition, 0, this);
for (int i = 0; i < toolbar->count(); ++i)
{
if (i != toolbar->currentIndex())
{
QWidget *w = toolbar->tabButton(i, closeSide);
w->hide();
}
}
hide() leaves empty space for the invisible close button. This looks funny.
Set the width to 0 instead.

QT Editor smart buttons

I have a toolbar with buttons: undo, redo, cut, copy, paste, like below:
editmenu = menuBar()->addMenu(QObject::tr("&Edit"));
undoact = editmenu->addAction(QIcon(":images/undo.png"), QObject::tr("&Undo"));
QObject::connect(editwin, SIGNAL(undoAvailable(bool)), undoact, SLOT(setEnabled(bool)));
QObject::connect(undoact, SIGNAL(triggered()), editwin, SLOT(undo()));
undoact->setShortcuts(QKeySequence::keyBindings(QKeySequence::Undo));
undoact->setEnabled(false);
redoact = editmenu->addAction(QIcon(":images/redo.png"), QObject::tr("&Redo"));
QObject::connect(editwin, SIGNAL(redoAvailable(bool)), redoact, SLOT(setEnabled(bool)));
QObject::connect(redoact, SIGNAL(triggered()), editwin, SLOT(redo()));
redoact->setShortcuts(QKeySequence::keyBindings(QKeySequence::Redo));
redoact->setEnabled(false);
editmenu->addSeparator();
cutact = editmenu->addAction(QIcon(":images/cut.png"), QObject::tr("Cu&t"));
cutact->setShortcuts(QKeySequence::keyBindings(QKeySequence::Cut));
cutact->setEnabled(false);
copyact = editmenu->addAction(QIcon(":images/copy.png"), QObject::tr("&Copy"));
copyact->setShortcuts(QKeySequence::keyBindings(QKeySequence::Copy));
copyact->setEnabled(false);
pasteact = editmenu->addAction(QIcon(":images/paste.png"), QObject::tr("&Paste"));
pasteact->setShortcuts(QKeySequence::keyBindings(QKeySequence::Paste));
I want:
Paste button to be enabled only if clipboard contain text
For some reason, I need to disable all buttons for a limitted period of time. After this I want to enable all buttons. But when I enable, each button to be enabled/disabled according to his condition (eg. undo button to be enabled only if there is something to undo, paste button to be enabled only if there is some text that can be pasted ...)
What do you advise me to do? What is the smartest / simplest method?
http://doc.qt.io/qt-5/qclipboard.html#signals
http://doc.qt.io/qt-5/qclipboard.html#details
Create connections from the QClipboard object to a handler slots in your toolbar/mainwindow class. And then in the handler slot do the logic you want to happen to your toolbar buttons.
For example:
http://doc.qt.io/qt-5/qclipboard.html#dataChanged
// in your constructor after creating your buttons:
QObject::connect(qApp->clipboard(), SIGNAL(dataChanged()),
this, SLOT(onClipboardDataChanged()));
// and then make another connection for the other relevant clipboard signal...
// selectionChanged()
Then later in in your slot:
void MainWindow::onClipboardDataChanged()
{
const QClipboard *clipboard = QApplication::clipboard();
const QMimeData *mimeData = clipboard->mimeData();
pasteact->setEnabled(mimeData->hasText());
// or you could check the length of the text to see if it is non zero.
}
To disable/renable all the buttons at once, disable the parent object instead.
editToolbar->setEnabled(false);
//... after some period of time or transactions...
editToolbar->setEnabled(true);
But this probably won't disable the built-in accelerators for Copy, Cut and Paste. To kill those keyboard shortcuts, too, you may need to put an event filter on your app.
http://doc.qt.io/qt-5/accelerators.html
http://doc.qt.io/qt-5/qobject.html#eventFilter
http://doc.qt.io/qt-5/qobject.html#installEventFilter
Hope that helps.
PS. I haven't tested this chunk of code. Read the docs. Good luck.

QDialog on accept return custom class object

I'm using qt-creator to build a little QT application.
I have a main window where I have some controls like a "new contact" button.
Pressing the button a QDialog is shown, it contains 3 line edits: name, mobile and email.
The dialog is shown through the Signal/Slot system. It works fine but I want to create a Contact object when OK is clicked and I want to give back that Contact to my main window in order to put it in a QList created in the main window code.
The approach is:
QMainWindow -> new contact -> QDialog is shown
QDialog -> ok -> QMainWindow
Should I pass the QList from the main window to the QDialog as argument or there is a best way?
Should I pass the QList from the main window to the QDialog as argument or there is a best way?
In my opinion, best would be a custom QDialog subclass with three QLabels and 3 QLineEdits.
The labels would get the following type of values:
Label 1: name
Label 2: mobileNumber
Label 3: email
You would use then QLabels and QLineEdits to display them with the input coming from the user.
Then, as your program probably already does, just handle the "accept" event respectively. You could use the following method to retrieve the text entered by the end user:
text : QString
This property holds the line edit's text.
Setting this property clears the selection, clears the undo/redo history, moves the cursor to the end of the line and resets the modified property to false. The text is not validated when inserted with setText().
The text is truncated to maxLength() length.
By default, this property contains an empty string.
Then, in the handler of the accepted signal, you could call three accessor methods, like:
QString name() const { return nameLineEdit->text(); }
QString mobileNumber() const { return mobileNumberLineEdit->text(); }
QString email() const { return emailLineEdit->text(); }
You could also store that in a dedicated structure depending on you build up your data representation, so the structure would be something like this:
struct Contact {
QString name;
QString mobileNumber;
QString email;
};
and then you would have the accessor for that as follows
Contact contact() const;
Make a subclass of QDialog. Call Yourclass::exec() to show the dialog (exec is a function in QDialog), then afterwards Yourclass::contactDetails() to get them. contactDetails is a perfectly ordinary member function that you have to write.

Qt - Set display text of non-editable QComboBox

I would like to set the text of a QComboBox to some custom text (that is not in the QComboBox's list), without adding this text as an item of the QComboBox.
This behaviour is achievable on an editable QComboBox with QComboBox::setEditText(const QString & text).
On a non-editable QComboBox, however, this function does nothing.
Is it possible to programmatically set the display/edit text of a non-editable QComboBox to something that is not in its list?
Or do I have to find another way (e.g. use a QPushButton with a popup menu)
EDIT: Consider an editable QComboBox with InsertPolicy QComboBox::NoInsert. If the user types in something and hits enter, the entered value will be used but not added to the list. What I want is this behaviour to change the 'current' text programmatically, but without allowing the user to type in some text himself. The user can choose something from the QComboBox, but some time later, I may want to override the 'current' text.
I had the same problem when I subclassed QComboBox to make a combo box of check boxes. I wrote a small function to programmatically change the text displayed in the combo box, but I didn't want to enable the user to edit that text. The solution was to set the combo box as editable:
this->setEditable(true);
and the QComboBox::lineEdit() to read only. Refer to the function:
void CheckedComboBox::setText(QString text)
{
QLineEdit *displayedText = this->lineEdit();
displayedText->setText(text);
displayedText->setReadOnly(true);
}
Reimplement paintEvent : https://github.com/qt/qtbase/blob/28d1d19a526148845107b631612520a3524b402b/src/widgets/widgets/qcombobox.cpp#L2995
and add this line : opt.currentText = QString(tr("My Custom Text"));
Example :
QCustomCheckComboBoxFilter.h
...
protected:
void paintEvent(QPaintEvent *e) Q_DECL_OVERRIDE;
...
QCustomCheckComboBoxFilter.cpp
...
void QCustomCheckComboBoxFilter::paintEvent(QPaintEvent *)
{
QStylePainter painter(this);
painter.setPen(palette().color(QPalette::Text));
// draw the combobox frame, focusrect and selected etc.
QStyleOptionComboBox opt;
initStyleOption(&opt);
opt.currentText = QString(tr("My Custom Text"));
painter.drawComplexControl(QStyle::CC_ComboBox, opt);
// draw the icon and text
painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
}
...
I supposed that you want to have a combo box with "A", "B", "C" as actual data and "This is A"
, "This is B" and "This is c" as what is displayed in QComboBox. Here is the code:
box.addItems(QStringList () << "This is A"<< "This is B"<< "This is C");
box.setItemData(0, "A");
box.setItemData(1, "B");
box.setItemData(2, "C");
You can get the actual data with this code :
QString actual = box.itemData(0).toString();//actual will be = "A";
qDebug()<<actual;//"A"
Note: You can almost set every data types that you want for a combo box Item. Even more, you can set more that just one additional data for each item with the third parameter of setItemData.
I ended up using a QPushButton with a popup menu.
I added the items I had in the list of my QComboBox as QActions to the menu.
A menu can be set on a QPushButton with
QPushButton::setMenu(QMenu* menu)
.
The text on the button can easily be set with
QPushButton::setText(const QString &)
and is unrelated to the text in the popup menu, which is what I wanted.

QTreeWidget editItem fails with "edit: editing failed"

I have a QTreeWidgetItem added to a QTreeWidget:
QTreeWidgetItem* item = new QTreeWidgetItem(ui->trwPairs);
item->setFlags(item->flags() | Qt::ItemIsEditable);
If the item is edited, I want to do a few checks on the new value:
Pairs::Pairs(QWidget *parent) :
QWidget(parent),
ui(new Ui::Pairs)
{
ui->setupUi(this);
connect(this->ui->trwPairs, SIGNAL(itemChanged(QTreeWidgetItem*,int)), this, SLOT(Validate(QTreeWidgetItem*,int)));
}
void Pairs::Validate(QTreeWidgetItem* item, int column)
{
if (item->text(column).toInt() < 1)
{
QMessageBox::critical(this, "Error", QString("Node ID ") + item->text(column) + " is invalid.");
ui->trwPairs->editItem(item, column);
}
}
Naturally, if it's less than 1, it catches it, and gives me the message box. However, printed to cerr is edit: editing failed and the item is not in edit mode. What am I missing?
Stepping through it in the debugger reveals the following:
In quabstractitemview.cpp line false is returned on line 3953. Somehow it looks like your item is still in editing state and you are trying to edit it again or something.
bool QAbstractItemViewPrivate::shouldEdit(QAbstractItemView::EditTrigger trigger,
const QModelIndex &index) const
{
// ..
if (state == QAbstractItemView::EditingState)
return false;
}
IIRC I had a similar problem with tables with multiple lines per cell. Check out the classes QAbstractItemDelegate views have item delegates which allow you to control which editor is used and how it behaves. I believe by default the QLineEdit is used. Editors like QLineEdit can have validators which control how the data is validated, in your case reject it if the numerical value is < 0. But I think you have to use the model / view classes and implement your own model for that. The Qt documentation for QTreeWidget::setItemWidget(..) says:
This function should only be used to display static content in the place of a tree widget item. If you want to display custom dynamic content or implement a custom editor widget, use QTreeView and subclass QItemDelegate instead.
I am not sure however if there is a simpler way to do this using the widget classes.
The problem could be, that you are setting the flags for your items in a very strange way.
Simply enable both item-selection, and edit:
item->setFlags(Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
I had a similar issue where I was attempting to edit the subsequent column upon receiving the itemChanged signal. Based on Nils' analysis that the item was still in the edit state, I changed the signal connection type to QueuedConnection, which allowed the item to leave the state before re-entering it.
I had a similar problem where I'd get the 'edit: editing failed' error when invoking edit() via a shortcut key. I was passing currentIndex() to edit(), but I wasn't checking that the correct column of the selected row was current. I only had the first column editable, so if I had clicked the row (but in any other column) and then invoked my edit key I'd get the error.
I was able to solve my problem by passing the result of sibling(currentIndex().row(), 0) to edit() instead.