Multiple buttons on click same function - c++

This is a follow up question of Efficient way to make an array of labels.
I have an array of buttons made by code (not designer) which are all added to a gridlayout. What I want is to be able to click any button on that gridlayout and call one same function with the row and column as parameters. Why I want this is because I do not feel like writing 15x15 functions which all do the same thing.
Is there a way or should I try to find another solution?
Ps. All my other input is made in the qt designer via "go to slot" so if it has to happen otherwise, I'll be clueless about how to.
Edit: The array of labels is now an array of buttons.

You could connect all of your buttons to a slot with no parameters and then get the position of the sender in this steps:
Cast the sender QObject to a QWidget via qobject_cast
Retrieve the index of that QWidget using QLayout::indexOf(QWidget *widget)
Then get the row, column, column span and row span with the QGridLayout::getItemPosition(int index, int *row, int *column, int *rowSpan, int *columnSpan)
The example code would look like this:
void MyWidgetWithAllLabels::commonSlot()
{
QWidget *buttonWidget = qobject_cast<QWidget*>(sender());
if (!buttonWidget)
return;
int indexOfButton = ui->gridLayout->indexOf(buttonWidget);
int rowOfButton, columnOfButton, rowSpanOfButton, columnSpanOfButton;
ui->gridLayout->getItemPosition(indexOfButton,
&rowOfButton, &columnOfButton, &rowSpanOfButton, &columnSpanOfLabel);
// Now you can get a reference to that specific QPushButton
QLayoutItem *item = ui->gridLayout->itemAtPosition(rowOfButton, columnOfButton);
QPushButton *clickedButton = qobject_cast<QPushButton*>(item->widget());
if (!clickedButton)
return;
// ... do something with that clickedButton
}
Referring to the code in your related post, you can connect your buttons to that slot like this:
connect( ui->tile_0_0, SIGNAL(clicked()),
this, SLOT(commonSlot()));
connect( ui->tile_0_1, SIGNAL(clicked()),
this, SLOT(commonSlot()));
// ...

By default, a QLabel has no "clicked" signal.
But you can do your own QLabel with 2 integers (row, col) and when you've got a mouseReleaseEvent (or mousePressEvent), you send a custom signal that looks like this: clicked(int row, int col).
You can also use a QSignalMapper:
http://qt-project.org/doc/qt-4.8/qsignalmapper.html#details

Related

QStyledItemDelegate partially select text of default QLineEdit editor

I have a subclass of QStyledItemDelegate which at the moment does not reimplement any functions (for simplicity of the question).
With default QStyledItemDelegate implementation, when the user begins to edit text in a QTableView, the delegate draws a QLineEdit with the text from the model, and selects all of it (highlights all for editing).
The text represents file names such as "document.pdf". The user is allowed to edit this entire text, however, I only want to initially highlight the base name portion ("document") and not the suffix ("pdf"). How can I do this? (I don't need the logic of how to do this, I need to know how to get the QStyledItemDelegate to highlight a portion of text)
I've tried:
in setEditorData() used QLineEdit::setSelection() to highlight some text. This has no effect.
in paint() attempted to paint based on what other respondents have recommended to similar questions, but not success. I have little experience with QPainter. Here is an example: Adjusting the selection behaviour of QStandardItem with QStyledItemDelegate
Please help, and thanks in advance. A code snippet with say selecting the first 3 characters of text would be greatly appreciated.
As noted in my comments to the question, the problem with subclassing QStyledItemDelegate and trying to set any default selection in setEditorData like this:
void setEditorData(QWidget* editor, const QModelIndex &index)const{
QStyledItemDelegate::setEditorData(editor, index);
if(index.column() == 0){ //the column with file names in it
//try to cast the default editor to QLineEdit
QLineEdit* le= qobject_cast<QLineEdit*>(editor);
if(le){
//set default selection in the line edit
int lastDotIndex= le->text().lastIndexOf(".");
le->setSelection(0,lastDotIndex);
}
}
}
is that (in Qt code) after the view calls our setEditorData here, it tries to call selectAll() here when the editor widget is a QLineEdit. That means that whatever selection we provide in setEditorData will be changed afterwards.
The only solution I could come up with, was to provide our selection in a queued manner. So that, our selection is set when execution is back into the event loop. Here is working example:
#include <QApplication>
#include <QtWidgets>
class FileNameDelegate : public QStyledItemDelegate{
public:
explicit FileNameDelegate(QObject* parent= nullptr)
:QStyledItemDelegate(parent){}
~FileNameDelegate(){}
void setEditorData(QWidget* editor, const QModelIndex &index)const{
QStyledItemDelegate::setEditorData(editor, index);
//the column with file names in it
if(index.column() == 0){
//try to cast the default editor to QLineEdit
QLineEdit* le= qobject_cast<QLineEdit*>(editor);
if(le){
QObject src;
//the lambda function is executed using a queued connection
connect(&src, &QObject::destroyed, le, [le](){
//set default selection in the line edit
int lastDotIndex= le->text().lastIndexOf(".");
le->setSelection(0,lastDotIndex);
}, Qt::QueuedConnection);
}
}
}
};
//Demo program
int main(int argc, char** argv){
QApplication a(argc, argv);
QStandardItemModel model;
QList<QStandardItem*> row;
QStandardItem item("document.pdf");
row.append(&item);
model.appendRow(row);
FileNameDelegate delegate;
QTableView tableView;
tableView.setModel(&model);
tableView.setItemDelegate(&delegate);
tableView.show();
return a.exec();
}
This may sound like a hack, but I decided to write this until someone has a better approach to the problem.

Qt adding "action" to dynamically added QWidget

In my ui I have a button, which adds QComboBox (with some items) and QLabel(with some text) when clicked. Variable "index" is the number of added QComboBoxes and QLabels. "ol" is qvector with some data.
void MainWindow::on_iAddOtherButton_clicked()
{
QComboBox *p1 = new QComboBox(this);
p1->setObjectName("comboBox"+QString::number(index));
QLabel *p2 = new QLabel(this);
p2->setObjectName("othLabel"+QString::number(index));
for(int i = 0; i < static_cast<int>(ol.size()); ++i){
p1->addItem(ol.at(i).getName());
ui->otherLayout->addWidget(p1,index+1,0);
}
p2->setText(...some text...));
ui->otherLayout->addWidget(p2,index+1,1);
index++;
}
And this works well, in the layout they are in pairs like this:
QComboBox1 QLabel1
QComboBox2 Qlabel2
Now I want to change value of the QComboBox1, after which, text of the QLabel1, will automatically change to something else.
I tried to do this with connect, but QComboBox on currentTextChanged() emits only QString with a new value. Is there some way to emit name of object + new value?
Or there is some completely other solution to do this?
From within your slot, you can call sender() to get a pointer to the sender of the signal. From there you can decide what to do next based on sender()->objectName().
You might also consider using setProperty(...) on the combobox objects to store an index to a vector of pointers to QLabel instances with the vector being a class member. Then you could retrieve the index inside your slot by calling sender()->property(...) and use it to access the correct QLabel widget from the vector.

How does QSignalMapper work?

After my post here : Associate signal and slot to a qcheckbox create dynamically I need to associate :
• The signal clicked() when I click on a qCheckBox to my function cliqueCheckBox(QTableWidget *monTab, int ligne, QCheckBox *pCheckBox)
To do so, I have to use QSignalMapper, after two hours of trying to understand how it works, I can't have a good result, here's the code I make, this is obviously wrong :
QSignalMapper *m_sigmapper = new QSignalMapper(this);
QObject::connect(pCheckBox, SIGNAL(mapped(QTableWidget*,int, QCheckBox*)), pCheckBox, SIGNAL(clicked()));
QObject::connect(this, SIGNAL(clicked()), this, SLOT(cliqueCheckBox(QTableWidget *monTab, int ligne, QCheckBox *pCheckBox)));
m_sigmapper->setMapping(pCheckBox, (monTab,ligne, pCheckBox));
QObject::connect(m_sigmapper, SIGNAL(clicked()),this, SLOT(cliqueCheckBox(QTableWidget *monTab, int ligne, QCheckBox *pCheckBox)));
Can you explain to me, how QSignalMapper works ? I don't really understand what to associate with :(
QSignalMapper class collects a set of parameterless signals, and re-emits them with integer, string or widget parameters corresponding to the object that sent the signal. So you can have one like:
QSignalMapper * mapper = new QSignalMapper(this);
QObject::connect(mapper,SIGNAL(mapped(QWidget *)),this,SLOT(mySlot(QWidget *)));
For each of your buttons you can connect the clicked() signal to the map() slot of QSignalMapper and add a mapping using setMapping so that when clicked() is signaled from a button, the signal mapped(QWidget *) is emitted:
QPushButton * but = new QPushButton(this);
QObject::connect(but, SIGNAL(clicked()),mapper,SLOT(map()));
mapper->setMapping(but, but);
This way whenever you click a button, the mapped(QWidget *) signal of the mapper is emitted containing the widget as a parameter.
First I will explain you how QSignalMapper works. Then I will explain you why you don't need it.
How QSignalMapper works:
Create s QSignalMapper. Lets assume that you want to assign an integer value to each checkbox, so every time you click on any checkbox, you will get a signal with the integer value assigned to it.
Connect the mapper signal to your SLOT, that you will implement:
connect(mapper, SIGNAL(mapped(int)), this, SLOT(yourSlot(int)));
Now you can write slot, that will take integer argument. The argument will be different for each checkbox you have.
While you create checkboxes, for each checkbox you need to do following:
mapper->setMapping(checkBox, integerValueForThisCheckbox);
connect(checkBox, SIGNAL(clicked()), mapper, SLOT(map()));
From now on, every time you click on a checkbox, it will emit clicked() signal to the QSignalMapper, which will then map it to the assigned integer value and will emit mapped() signal. You connected to that mapped() signal, so yourSlot(int) will be called with the proper integer value.
Instead of integers, you can assign QString, QWidget* or QObject* (see Qt documentation).
This is how QSignalMapper work.
You don't need it:
The QTableWidget *monTab is the single object, it doesn't change. Keep it as a class member field and use it from your slot function.
The QCheckBox *pCheckBox - you can get it by casting sender() to QCheckBox*.
Like this:
void supervision::yourSlot()
{
QCheckBox* pCheckBox = qobject_cast<QCheckBox*>(sender());
if (!pCheckBox) // this is just a safety check
return;
}
The sender() function is from QObject, which you do inherit from, so you have access to it.
The int linge (it's a line number, right?) - when you create checkboxes, you can store pointers to that checkboxes in QList class field and use it from your slot function find out which line is it, like this:
In class declaration:
private:
QList<QCheckBox*> checkboxes;
When creating checkboxes:
QCheckBox* cb = new QCheckBox();
checkboxes << cb;
In your slot function:
void supervision::yourSlot()
{
QCheckBox* pCheckBox = qobject_cast<QCheckBox*>(sender());
if (!pCheckBox) // this is just a safety check
return;
int linge = checkboxes.indexOf(pCheckBox);
}
If you want, you can skip that QList and use QSignalMapper and assign lines to checkboxes using mapper. That's just a matter of what you prefer.

Ktorrent-like widgets in QTableView/QTableWidget

Is there any documentation about how to put a custom QWidget container (with other stuff like a layout, checkboxes, buttons etc) as a row in a QTableView/QTableWidget like is shown in the picture?
I'm browsing the source code of ktorrent to see how this is done.
Any help will be much appreciated.
For a QTableWiget use
void QTableWidget::setCellWidget (int row, int column, QWidget *widget)
In your case:
class MyWidget : public QWidget {
// a composite widget with layouts and other stuff
};
setCellWidget(0, 0, new MyWidget);
In the case of a QTableView you have to define your custom delegate and set it (e.g.) for a certain column with:
void QAbstractItemView::setItemDelegateForColumn (int column, QAbstractItemDelegate * delegate)
Check this out for an example of a QProgressBar inside a table

Get previous value of QComboBox, which is in a QTableWidget, when the value is changed

Say I have a QTableWidget and in each row there is a QComboBox and a QSpinBox. Consider that I store their values is a QMap<QString /*Combo box val*/,int /*spin box val*/> theMap;
When comboBoxes value or spin boxes value is being changed I want to update theMap. So I should know what was the former value of the combo box in order to replace with the new value of the comboBox and also take care of the value of the spin box.
How can I do this?
P.S. I have decided to create a slot that when you click on a table, it stores the current value of the combo box of that row. But this works only when you press on row caption. In other places (clicking on a combobox or on a spinbox) itemSelectionChanged() signal of QTableWidget does not work.
So in general my problem is to store the value of the combo box of selected row, and the I will get ComboBox or SpinBox change even and will process theMap easily.
How about creating your own, derived QComboBox class, something along the lines of:
class MyComboBox : public QComboBox
{
Q_OBJECT
private:
QString _oldText;
public:
MyComboBox(QWidget *parent=0) : QComboBox(parent), _oldText()
{
connect(this,SIGNAL(editTextChanged(const QString&)), this,
SLOT(myTextChangedSlot(const QString&)));
connect(this,SIGNAL(currentIndexChanged(const QString&)), this,
SLOT(myTextChangedSlot(const QString&)));
}
private slots:
myTextChangedSlot(const QString &newText)
{
emit myTextChangedSignal(_oldText, newText);
_oldText = newText;
}
signals:
myTextChangedSignal(const QString &oldText, const QString &newText);
};
And then just connect to myTextChangedSignal instead, which now additionally provides the old combo box text.
I hope that helps.
A bit late but I had the same problem and solved in this way:
class CComboBox : public QComboBox
{
Q_OBJECT
public:
CComboBox(QWidget *parent = 0) : QComboBox(parent) {}
QString GetPreviousText() { return m_PreviousText; }
protected:
void mousePressEvent(QMouseEvent *e)
{
m_PreviousText = this->currentText();
QComboBox::mousePressEvent(e);
}
private:
QString m_PreviousText;
};
My suggestion is to implement a model, which would help you make a clean separation between the data, and the UI editing the data. Your model would then get notified that a given model index (row and column) changed to the new data, and you could change whatever other data you needed to at that point.
I was just having a similar issue, but for me i needed the previous index for something very trivial so defining and implementing a whole class for it was unjustified.
So what I did instead was keep an argument called say 'previousIndex' and updated it's value only after I had done everything I needed with it