connect QPushButton and QComboBox - qt

How can I connect a QPushButton and a QComboBox?
I created a SLOT that accepts 2 parameters, a pointer to the QComboBox and the index of the selected item:
void modificaExp::eliminaExp(QComboBox *combo,int value)
{
......
combo->removeItem(value);
....
}
the widgest are there:
QComboBox* combo=new QComboBox();
combo->addItem("ciao1");
combo->addItem("ciao44");
combo->addItem("ciao222");
combo->addItem("ciao555");
QPushButton* delButton=new QPushButton();
delButton->setText("delete");
connect(delButton, SIGNAL(clicked()), this, SLOT( eliminaExp(combo,combo->currentIndex() )));
so, when I click on delButton the element stays there. I think there is a problem in the connect command, specifically I think than the slot is not called.

Are you sure you need this slot with two parameter?
Another simple way:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
setupUi(this);
connect(deleteButton, SIGNAL(clicked(bool)), this, SLOT(deleteSlot()));
}
void MainWindow::deleteSlot()
{
comboBox->removeItem(comboBox->currentIndex());
}

The slot should have the same type and equal or less number of arguments than the signal
Declare the QComboBox and the QPushButton objects in the header
modificaexp.h
private:
QComboBox* combo;
QPushButton* delButton;
modificaexp.cpp
combo=new QComboBox();
combo->addItem("ciao1");
combo->addItem("ciao44");
combo->addItem("ciao222");
combo->addItem("ciao555");
delButton=new QPushButton();
delButton->setText("delete");
connect(delButton, SIGNAL(clicked()), this, SLOT( eliminaExp()));
Modify the slot
void modificaExp::eliminaExp()
{
combo->removeItem(combo->currentIndex());
}
Refer the Qt signal slot documentation

Related

Problem with connecting QLabel to QSlider

I have a problem. I've created a class, in which I have a slider and a label. I want to connect these with the QObject::connect, but when I do it, nothing happens. Can you tell me what am I doing wrong?
My class:
class Loads :public QObject
{
Q_OBJECT
public:
QSlider slider;
QLabel label;
QMainWindow okno;
Loads();
private:
int wart;
public slots:
void zmiana(int li);
};
Class "Loads" constructor:
Loads::Loads()
{
okno.setGeometry(300,300,300,300);
label.setParent(&okno);
slider.setParent(&okno);
label.setGeometry(0,0,300,200);
slider.setGeometry(0,200,300,100);
slider.setMinimum(1);
slider.setMaximum(30);
label.setText("0");
wart=0;
QObject::connect(this, SIGNAL( slider.valueChanged(int)), this , SLOT( zmiana(int)) );
okno.show();
}
My "zmiana" slot
void Loads::zmiana(int li)
{
wart=li;
label.setText(QString::number(li));
}
QObject::connect(this, SIGNAL( slider.valueChanged(int)), this , SLOT( zmiana(int)) );
I don't think that's correct, you're connecting the signal of the Loads object to the slot but the Loads object is not the one generating the signal, the slider object is doing that.
Hence I think you'll need slider as the first argument, not this. Using this as the third argument is okay, I believe, since the slot does belong to the Loads object.

How to add a QAction to a QListWidget

I have the following code:
roslaserscandoialog.h
public:
explicit ROSLaserScanDialog(QWidget *parent = nullptr);
~ROSLaserScanDialog();
QListWidgetItem *createItemFromAction(const QAction* action);
private slots:
void on_listWidget_itemClicked(QListWidgetItem *item);
private:
Ui::ROSLaserScanDialog *ui;
QAction *mAddMsgs;
QAction *mDeleteMsgs;
roslaserscandoialog.cpp
ROSLaserScanDialog::ROSLaserScanDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::ROSLaserScanDialog)
{
ui->setupUi(this);
connect(ui->listWidget,SIGNAL(on_listWidget_itemClicked(QListWidgetItem*)),this,SLOT(createItemFromAction(QListWidgetItem*)));
}
QListWidgetItem *ROSLaserScanDialog::createItemFromAction(const QAction *action)
{
Q_ASSERT( action );
QListWidgetItem *mAddMsgs = new QListWidgetItem();
mAddMsgs->setText( action->text() );
mAddMsgs->setToolTip( action->toolTip() );
mAddMsgs->setIcon( action->icon() );
// ...
return mAddMsgs;
}
void ROSLaserScanDialog::on_listWidget_itemClicked(QListWidgetItem *item)
{
mAddMsgs = new QAction(QIcon(":ros.png"), tr("Add New Message"), this);
mDeleteMsgs = new QAction(QIcon(":remove_item.png"), tr("Remove Message"), this);
}
What I have done so far:
I came across this post and also this one. Very useful as I set up my project in a very similar way, but nothing happens when I try to click on the QListWidget.
I know that in order to trigger the action I have to go to the slot called itemClicked as I did on the above code provided.
On the official documentation I was trying to apply what is advised but I don't know why nothing happens.
Please point to the right direction for solving this problem.
Look at console output, there should a warning about connect failing. If you look at your code, the reason should be pretty obvious. Consider
SLOT(createItemFromAction(QListWidgetItem*))
versus your method which isn't even a slot
QListWidgetItem *createItemFromAction(const QAction* action);
See the difference?
And then you have this slot:
void on_listWidget_itemClicked(QListWidgetItem *item);
which you are trying to use as a signal
SIGNAL(on_listWidget_itemClicked(QListWidgetItem*))
That obviously won't work.
It's a bit unclear what you want to happen when an item is clicked, but maybe you just should call createItemFromAction directly from the on_listWidget_itemClicked.
Additionally, add debug print or use breakpoint to verify that the on_listWidget_itemClicked is actually called when you click an item. If not, then you are missing connecting the relevant signal from your list view, ie. ui->setupUi(this); does not have that connect (in other words you did not do the connection in the GUI Designer).

signal points to slot in struct

I'd like a signal to be connected to a slot inside of a struct. My struct looks like:
//Header file
struct someStruct {
public:
int index;
public slots:
void someSlot();
};
QList<someStruct*> mListOfStructs;
and then I create a button that should forward its clicked() signal to the someSlot function.
//Source file
QPushButton *cmd = new QPushButton();
grd->addWidget(cmd, 3, 2, Qt::AlignCenter);
//grd is a QGridLayout somewhere inside the gui. I can see it and also the button.
now connection the clicked() event with the slot inside a specific struct does not work.
connect(cmd, SIGNAL(clicked()), mListOfStructs[3], SLOT(someSlot()));
some sources tell me that I have to add a metaObject or sth. I tried but it didn't work out. Maybe you know better.
I might use How to connect in Qt signal and slot in dynamically added buttons to get in slot index of added button? as workaround though.
your structure need the Q_Object meta attributes in order to emit signals and recieve slot events...
struct testStruct : public QObject
{
Q_OBJECT
public:
int index;
testStruct():index(0){}
public slots:
void someSlot()
{
qDebug() << "slot called!";
}
};
after that you can conected as usual:
testStruct *ts= new testStruct;
connect(this, SIGNAL(someSignal()), ts, SLOT(someSlot()));

Work around for signal and slot argument limitation

I want to make a PushButton when it is clicked, its text change into 'clicked'. I tried it by
connect(button1, SIGNAL(clicked()), this, SLOT(markClicked(button1)));
where this refer to the MainWindow and
void MainWindow::markClicked(QPushButton *button) { button->setText("Clicked"); }
It does not seem to work because I think SLOT cannot take more arguments than SIGNAL. If there any approach to work around this limitation?
Thanks.
Qt signals/slots mechanism can only transfer signal to slot function with similar parameters. As a workaround, you should use QSignalMapper:
QSignalMapper mapper;
...
connect(button1, SIGNAL(clicked()), &mapper, SLOT(map()));
mapper.setMapping(button1, button1); // not sure whether this is mandatory or not
...
connect(&mapper, SIGNAL(mapped(QWidget*)), this, SLOT(markClicked(QWidget*)));
and function markClicked is
void MainWindow::markClicked(QWidget *widget) {
QPushButton *button = qobject_cast<QPushButton*>(widget);
button->setText("Clicked");
}
The other way you could do this is to use a default value for the argument and then use the sender() method:
In MainWindow:
void markClicked(QPushButton *button = NULL);
then:
connect(button1, SIGNAL(clicked()), this, SLOT(markClicked()));
and:
void MainWindow::markClicked(QPushButton *button) {
if (button==NULL) { button = qobject_cast<QPushButton*>(sender()); }
button->setText("Clicked");
}

how to add menu dynamically in Qt

I want to add, submenu to a menu item dynamically. How can I achive this?
I tried like this,
I have created an Action and submenu. Then I have added the submenu to action.
But, I have connected the “triggered” signal of action. its getting crash if I click on the action..
I have also handled the “aboutToShow” signal of menu, same its also getting crash when I click on action..
Here is the sampe code.
Submenu = new QMenu(this);
connect(Submenu, SIGNAL( aboutToShow()), this, SLOT(move ()));
QAction *test = new QAction(tr("Selection"), this);
test ->setMenu(Submenu);
menubar()->addAction(test);
I want to get the notification, before the display of submenu..
additonal information:
pleas try this code, in your main window constructor.
QAction *action = new QAction("Test",this);
QAction *dummyaction = new QAction("Testing",this);
QMenu *menu = new QMenu();
menu->addAction(dummyaction);
bool val= connect(menu, SIGNAL( aboutToShow()), this, SLOT( Move()));
val= connect(menu, SIGNAL( aboutToHide()), this, SLOT(Move()));
action->setMenu(menu);
this->menuBar()->addAction(action);
if I do like this, I am able to see one submenu item. But before that Move slot should call, its not getting called.. and even before hide also the same slot should call.. its not coming..
I tried the return values of connect.. its true only… so what is wrong with my code.. please say..
Such code should work:
QMainWindow wnd;
QAction *act = wnd.menuBar()->addMenu("SomeMenu")->addMenu("someSubmenu")->addAction("someAction");
QObject::connect(act,SIGNAL(triggered()),
someObj,SLOT(actionReaction()));
I think addMenu() addAction() should work in more reliable way. This approach works for me.
I'm not sure to understand exactly what you're willing to do into your Move() slot.
But here is your own code (I removed what seemed useless to me), modified so that it is not crashing on my computer :
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QAction>
#include <QMenu>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
private:
QMenu* menu;
QAction *dummyaction;
QMenu* m_pSubMenu;
private slots:
void Move();
};
#endif // MAINWINDOW_H
mainwindow.cpp :
#include "mainwindow.h"
#include <QMenuBar>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
m_pSubMenu = NULL;
QMenuBar* pMenuBar = new QMenuBar(this);
setMenuBar(pMenuBar);
dummyaction = new QAction("Testing",this);
menu = new QMenu("Test", this);
menu->addAction(dummyaction);
this->menuBar()->addMenu(menu);
connect(menu, SIGNAL(aboutToShow()), this, SLOT(Move()));
}
void MainWindow::Move() {
if (!m_pSubMenu) {
m_pSubMenu = new QMenu(menu);
dummyaction->setMenu(m_pSubMenu);
}
QAction* pAction = new QAction("Test", this);
m_pSubMenu->addAction(pAction);
}
I don't know exactly what you want to do into your Move() slot, but as an example, each time the Move() slot is called, a new submenu item is added.
Hope this helps.

Resources