Assign new slot clicked() for button - qt

I have copied several buttons in design form. As result all buttons got the same slot for clicked(). How to create and assign new personal slots for buttons?
UPD
Trying to use Signals & Slots editor. It was empty in the beginning, and I added new one:
But if I right click on button btnInfoCB->go to slot.. clicked() , it will point me to the same old method.

Qt creator will create slots for you. From designer, Right click on any push button and select 'Go to slots' option. select Clicked() signal press OK. Qt creator will navigate you to the created slot. Qt automatically connects your button clicked signal to the created slots.
You can do the same for all push buttons you created

Qt has an auto-connect feature that connects the slots named like this: on_UIELEMENTNAME_SIGNALNAME(SIGNAL_PARAMS) with the corresponding signal of the ui element, see the documentation here
So to fix this you have 2 options:
Option 1: rename the slots to have all the names corresponding with the names from .ui file:
private slots:
void on_MyFirstBtn_clicked();
void on_MySecondBtn_clicked();
//and so on
Or use option 2: manually connect
private slots:
//notice the prefix "on_" and the suffix "_clicked()" not part of the function name
void MyFirstBtnClicked();
void MySecondBtnClicked();
And in the constructor of the class (i named it MyWidget) add the connect calls:
connect(ui->MyFirstBtn, &QPushButton::clicked, this, &MyWidget::MyFirstBtnClicked);
connect(ui->MySecondBtn, &QPushButton::clicked, this, &MyWidget::MySecondBtnClicked);
LE: I don't recommend using the UI Designer for connecting slots that are defined in code because of maintainability reasons. That feature in Designer is very good if you need to connect already defined signals and slots, like check/unched to show/hide and other similar predefined functionality, but for your defined slots i don't like it.

New slot for an existing QPushButton /w auto connection
You have to change the objectName of QPushButton first. Then right click and create a new slot connection e. g. clicked(). Qt Creator will then create a new slot with the new objectName e. g. void on_newObjectName_clicked() { }. The old slot is no longer used then and you will get a new fresh function.
Manually connect /w signals and slots editor
If you like to connect it through your editor, you first have to click at the QWidget (e. g. MainWindow) and select change signals/slots from the menu. There you have to add a new function e. g. myNewSlot(), which is then available in the drop-down in the signals and slots editor as shown in your screenshot. If you use this type of connection you need to declare the slot in your .h in private slots and in the .cpp file. The object name may still link to an auto connection, so you probably need to change the objectName as well or remove the auto connected slot.

Related

Qt Signals and Slots between Forms

How do I connect a Signal from a form to a Slot in another form? In other words I have a value that I am manipulating in a dialog.ui file that I want to emit to a slot that changes the value in the main application.
go to controller of the main GUI and connect both of them , like :
connect(form1,SIGNAL(increment()),form2,SLOT(manipulate()));
don't forget to use emit in the first form.

Function connected to the button is called twice after one click

I have a problem with slots and signals. I created buttons and connected them to the clicked() slot. Then i decided to connect signals and slots manually and since then when I click the button it calls its function twice.
connect(ui->okButton, SIGNAL(clicked()), this, SLOT(on_okButton_clicked()));
void settingswindow::on_okButton_clicked()
{
qDebug() << "ok clicked";
this->close();
}
I was looking for the answer on google, but all i found was this: Where is the generated code of qt signals slots editor but my *.ui file looks like this: pastebin to the code. As you can see there's only one line with and nothing more. I can't find where the information about signals and slots is saved. Rebuild and clean options won't help.
This is not a bug in Qt. If you look at the generated code for your ui_*.h file, you'll notice that the last statement executed in the setupUi() function is a call to QMetaObject::connectSlotsByName().
Since your slot already conforms to the naming convention that this function is looking for, your slot is automatically connected to the signal.
By manually connecting the signal to the slot, in your settingswindow class, you effectively duplicate the connection.
As #Devopia mentioned, this is a documented feature.

Qt button assign command

I'm using qt creator to create a gui for an vtk aplication.
I've made a #define action and I want to assign that command to a QPushButton.
How do I make the button reproduce my define if pressed?
example.
ui->pushButton->...
The easy way is to define a default reponse slot for your button. In your class definition (the same class that defines the pushButton) you can just create a default slot under the slots section.
class MyMainApplication : public QMainWindow
{
Q_OBJECT
public:
MyMainApplication(QWidget *parent = 0);
~MyMainApplication();
// More stuff in the public section (...)
public slots:
void on_pushButton_clicked(); // This is the important line
// The remaining definitions in your class
};
Now you just program the on_pushButton_clicked() function in your class implementation and it will respond to the click on the button called pushButton.
The syntax for default slots is on_NAMEOFTHEWIDGET_NAMEOFTHESIGNAL.
With the default slots, the connection is made automatically. If you want to use a generic name for the slot you must make the connection manually. For instance, say that instead of
on_pushButton_clicked() you want to use mySlot(). In the class definition you would type
(...)
public slots:
void mySlot();
(...)
and in the class implementation, probably in the constructor, after the ui->setupUi(this); command you would make the connection of the button and your slot:
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(mySlot()));
A final remark: in this particular case, I am almost sure that the slots can also be private.
You need to use Qt's feature called Signals and Slots. Many objects can send signals when interacted with. Example is "clicked()" signal emitted from a QPushButton, or "triggered()" which is emitted upon clicking on QAction.
Main idea is to connect a signal to a slot. Slots are functions which are called when a signal is emitted. I can provide you with a PyQt example:
QtCore.QObject.connect(self.ui.myButton, QtCore.SIGNAL("clicked()"), self.someFunction)
C++ code shouldn't be much different. Check the Qt examples, they provide lots of good stuff.

nube: stdio to QTextedit

I've read lots of similar threads to this one but im not such a great programmer that i can make sense of it all. Im using qtcreator to make life simple and want to maka a program that can trigger another process, monitor its stdout and then kill it if necessary.
What I assume i want to do is create a QTextedit in the designer and plug a signal into it that updates the contained text whenever the stream updates, so far so good, but thats where i get fuzzy. My initial thought was to create a subclass of QObject that starts the process as a QProcess and whenever the stdout updates the QObject and appends new data to the QTextedit box.
So my program structure would run like this:
on button press create new QObject derived class.
The QObject derived class constructor starts a QProcess and connects the readyReadStdout() signal to the derived qObject class slot.
When the derived QObject is triggered it takes readyReadStdout() and appends any new data to the QTetEdit box.
on button press, call the derived QObject destructor and which kills the process.
Has anybody done something similar? Like i say ive read similar posts but sometimes it takes asking a question in your own words to be able to understand it
Thanks everyone (also my forst post, woo:)
Ok so heres my update:
I have added an instance of QProcess class (named proc) to my mainWindow class and also new instance of a QObject derived class (named procLog) to which I added a slot. I want this slot to take the readyReadStandardOutput() signal as a trigger to call readAllStandardOutput() and emit the new line to a new signal in procLog, I'm having trouble connecting the QProcess slot to the QObject derived class. heres what I'm trying:
connect(proc, SIGNAL(readyReadStandardOutput ()), procLog, SLOT(logReady()));
I get, error: QObject::connect: Cannot connect (null)::readyReadStandardOutput () to (null)::logReady()
Do you know why this is. Aslo is there a way to add code blocks to text in the comments?
thanks!

QT signals & slots

How to use signal and slots for handling events i.e when a pushButton is clicked some number should be displayed in QLineEdit.Just like in caluculator application when a number is clicked the corresponding is displayed .
connect a widget's signal to other's widget slot.
Signal = Emitted when the button (for instance) is pressed.
Slot = called when a certain singal is fired, and this signal is connected to this slot.
If You use QtCreator here is the procedure:
right click on the pushbutton. Select go to slot.
Select clicked()
QtCreator will open C++ editor with new empty member function defined that implements
your slot. This function will be called whenever someone clicks this button.
Now in this function you can call method of another widget ot modify its contants.
This book is quite good for starters:
http://www.amazon.com/Book-Qt-Art-Building-Applications/dp/1593271476/ref=sr_1_8?ie=UTF8&qid=1300905855&sr=8-8
Good luck.

Resources