Qt connecting two signals and one slot - c++

I have a programm with a QLabel, QTextEdit and a QPushButton.
I want to put the text from LineEdit to Label when I click the button.
I can do that by creating my own slot but can it be done with Qt slots?
I've tried this code but it works not as I want...
this->connect(pushButton ,SIGNAL(clicked()), lineEdit, SIGNAL(textChanged(QString)), Qt::QueuedConnection);
t->connect(lineEdit, SIGNAL(textChanged(QString)) , label ,SLOT(setText(QString)), Qt::DirectConnection);

If you need to force the user to push a QButton for "applying" the text he/she typed in a QTextEdit to a QLabel, maybe you want to check the validity of the inserted text, or use the text to achieve some goal or to store it in a variable for later use... so you need a custom slot or a custom class.
Instead you can connect the signal QTextEdit.textChanged(QString) to the slot QLabel.setText(QString), so everything is typed in the QTextEdit is sent to the QLabel without pushing a button.
But all depends on your aim.

Here's how I would do it:
connect(ui->pushbutton, SIGNAL(clicked()), this, SLOT(slot_pushbutton_clicked()))
And then in the the slot_pushbutton_clicked slot,
ui->label->setText(ui->lineEdit->text)
Hope it helps :)

Related

Access Qt UI of another Class from the MainWindow Class

I want to understand if the following sequence is possible? If yes, how can we achieve the same?
MainWindow Qt GUI has a QPushButton
While we click on the QPushButton, it must open another Qt GUI Window (a different class, say 'DialogClass')
In the newly opened Qt GUI Window we have a QLineEdit and QPushButton
While we enter data in the QLineEdit and click on the QPushButton (of the DialogClass) the MainWindow class should receive the data entered in QLineEdit
Any help on this item will be appreciated. Thanks in advance!
Qt foresees its signals and slots approach for such purposes.
The QPushButton of your class offers a signal clicked which you connect to a custom (self-written) slot of your dialog. The dialog's slot then should read the contents of the QLineEdit and publish these on the dialogs own (custom) signal, which is connected to a (custom) slot of your main window, which then can process the value originally contained in the line edit.
Details will resemble pretty much the example of Qt's signals and slots documentation, so I won't be more explicit about.

Signal for scrollbar value changed in qtextedit

I am practicing some GUI programming. I would like to scroll 2 qtextedit at the same time.
my problem is i could not find the SIGNAL for verticalscrollbarvaluechanged.
connect(ui->textEdit,SIGNAL(....),this,SLOT(scroll());
i have this code for the SLOT
void MainWindow::scroll()
{
ui->textEdit->verticalScrollBar()->valueChanged(ui->textEdit2->verticalScrollBar()->value());
ui->textEdit2->verticalScrollBar()->valueChanged(ui->textEdit->verticalScrollBar()->value());
}
also is there a way to hide the scrollbar? just make it look transparent, but it still there?
thank you
You must not create a new slot, you must use the signal of a scrollbar with the slot of the other and vice versa.
connect(ui->textEdit->verticalScrollBar(), SIGNAL(sliderMoved(int)), ui->textEdit_2->verticalScrollBar(), SLOT(setValue(int)));
connect(ui->textEdit_2->verticalScrollBar(), SIGNAL(sliderMoved(int)), ui->textEdit->verticalScrollBar(), SLOT(setValue(int)));

How to pass a variable from main to signal and slot macros?

Having the hardest time setting up signal and slot macros for variables in main. It is extremely easy to do when the variables are located in classes, but how do you do this when you want to connect a variable in main?
I have two radio buttons in main as follows:
QRadioButton *btn_ledWhite = new QRadioButton;
QRadioButton *btn_ledBlack = new QRadioButton;
I want to pass these buttons to a function that sets their stylesheet. Something like below:
btn_led->setStyleSheet("QRadioButton::indicator::unchecked{background-color:gold;}");
When the user of my application presses btn_start, the white player's LED should light up. Unfortunately, I cannot pass the buttons from main to signal and slot macros. I want something like this:
QObject::connect(btn_start, SIGNAL(clicked()), whiteClock, SLOT(updateLED(btn_ledWhite)));
This is illegal qt syntax, however. Apparently, you cannot pass an argument to a function wrapped in a SLOT macro.
You can do something like:
Counter a, b;
QObject::connect(&a, SIGNAL(valueChanged(int)),
&b, SLOT(setValue(int)));
... but I cannot mix the presentation (GUI) layer, with the business layer (i.e. standard 3-tier architecture model... think MVC). Else I would just stick this button in a class and not worry make this thread.
Does anyone have idea how to connect a variable in main with a signal and slot macro?
You can use a QSignalMapper for this.
You connect your buttons' clicked() signal to the mapper's map() slot, then set the mapping from button to led with the mapper's setMapping function.
Once that's done, connect the mapper's mapped signal to your whiteClock. You might need to adjust your slot function's signature to take a QWidget rather than a QPushButton, but if all you need is to call setStyleSheet, then that's not much of a problem.
This code works. You do need to adjust the function signature as previously mentioned to a QWidget*, instead of a QRadioButton*...but everything else should be the same.
main.cpp
QSignalMapper * signalMapper = new QSignalMapper;
//Start game, start white's clock, turn on white's LED
QObject::connect(btn_start, SIGNAL(clicked()), whiteClock, SLOT(startClock()));
QObject::connect(signalMapper, SIGNAL(mapped(QWidget*)), whiteClock, SLOT(updateLED(QWidget*)));
QObject::connect(btn_start, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(btn_start, btn_ledWhite);
Set the stylesheet in your clock class and you're good to go.

Make QWidget disappears after click not on it

I'm showing QTableWidget, and want it to disappear after some item in this table was selected and if user clicked outside QTableWidget area.
connect(tableWidget, SIGNAL(itemClicked(QTableWidgetItem *)), tableWidget, SLOT(close()));
this line do what I want after selecting item. Is it a way of make widget disappears after clicking not on it without subclassing it (I can subclass and write my own losefocus event handler, for example, but without subclassing would be better)?
let's assume, you have MainWidget, that contains everything within it. It has clicked() signal. Connect that to some slot and in that slot hide tableWidget ( tableWidget()->hide() )
This is probably not a very elegant solution, but it might work:
Subclass QTableWidget
Make sure that the table widget has the keyboard focus while you display it.
Reimplement void QWidget::focusOutEvent ( QFocusEvent * event ) (close the table widget, when you lose focus)
My solution was to put QTableWidget into QWidget and made the latter Qt::Popup - an it acts exactly how I need

Problem with event handling on QToolButton in Linux

I am developing an application, I have added a QToolBar object in that, and have added the QToolButton object on that, I have also connect the clicked() event with that but the problem is that the mouse click event don't work on QToolButton but when I bring focus on that using Tab, then space button works fine, but I want it with mouse click.. any idea? here is the code.
pToolBar = new QToolBar(this);
pToolBar->setAllowedAreas(Qt::NoToolBarArea);//NoToolBarAreaAllToolBarAreas
pToolBar->setFloatable(false);
pToolBar->setGeometry(300,0,160,30);
QToolButton *playButton=new QToolButton(pToolBar);
playButton->setIcon(QIcon("/images/play.png"));
playButton->setGeometry(10,0,40,30);
playButton->setToolTip("Play/Pause");
connect(playButton, SIGNAL(clicked()),SLOT(playButtonClicked()));
The Tool buttons are normally created when new QAction instances are created with QToolBar::addAction() or existing actions are added to a toolbar with QToolBar::addAction().
Example:
QAction *newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this);
newAct->setShortcut(tr("Ctrl+N"));
newAct->setStatusTip(tr("Create a new file"));
connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
fileToolBar = addToolBar(tr("File"));
fileToolBar->addAction(newAct);
You can use triggered signal, This signal is emitted when the given action is triggered.
Your example:
QToolButton *playButton=new QToolButton(pToolBar);
connect(playButton, SIGNAL(triggered()),SLOT(playButtonClicked()));
Try explicitly adding the toolbutton to the toolbar. The following code works perfectly for me:
QToolBar *pToolBar = new QToolBar(this);
QToolButton *playButton=new QToolButton(pToolBar);
playButton->setIcon(QIcon("/images/play.png"));
playButton->setText("Play");
playButton->setToolTip("Play/Pause");
playButton->setGeometry(10,0,40,30);
QAction *a = pToolBar->addWidget(playButton);
a->setVisible(true);
connect(playButton, SIGNAL(clicked()),SLOT(playButtonClicked()));
You should probably save the QAction pointer somewhere, since it's the easiest way to assign keyboard shortcuts, enable / disable the button etc. Let me know if this works for you. If it doesn't, perhaps posting a complete compilable example here will help us help you. You should be able to get a small demo program that shows your problem within one or two files.
Cheers,
As jordenysp indirectly explains, the API is QAction centric