How can I connect a key enterPressed() event in my main GUI window to a specific button (also in main GUI)?
Usecase:
User starts the application, enter some data and press the Enter button and will be bypassed to a "Start" button is clicked.
My idea:
connect(this, SIGNAL(returnPressed()), ui.btn_Start, SLOT(clicked()));
class ClassA : QMainWindow
{
Q_OBJECT
// ...
private:
Ui::ClassAClass ui;
// ...
};
You can connect signals to other signals:
connect(this, &MainWindow::returnPressed, ui.btn_Start, &QPushButton::released);
And that is it. The signal is forwarded to QPushButton's instance signal emitting it so whatever is connected to it will be invoked. Additionally (you talk about events) in QMainWindow::keyPressEvent you re-implement:
class MainWindow : public QMainWindow
{
//...
signals:
void returnPressed() const;
protected:
void keyPressEvent(QKeyEvent *event);
};
void MainWindow::keyReleaseEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_Return)
emit returnPressed();
QMainWindow::keyReleaseEvent(event);
}
Related
There is no signal as clicked for QlineEdit.
connect(w,SIGNAL(clicked()),this,SLOT());
There is no such signal in Qt widgets.
You need to derive QLineEdit class and implement void QLineEdit::mouseReleaseEvent(QMouseEvent *e), like this:
class MyLineEdit public QLineEdit {
...
virtual void mouseReleaseEvent(QMouseEvent *e) { emit clicked(); }
signals:
void clicked();
}
Later, you can promote QLineEdit in graphical editor to MyLineEdit class.
I have a subclass of QGraphicsView to recognize mouse events, and it does. But when those mouse events happen, I need to call some other function in a different class, to handle how the mouse event interacts with the scene.
//Subclass
class Drawspace : public QGraphicsView {
public:
Drawspace(QGraphicsScene * scene, QWidget * parent) : QGraphicsView(scene, parent) {}
Drawspace(QWidget* parent) : QGraphicsView(parent) {}
void mousePressEvent(QMouseEvent * event) {
QMessageBox::information(this, tr("Dialog"), "You clicked the board (from QGraphicsView)");
QWidget::mousePressEvent(event);
}
};
//mainwindow code (my subclass is called "board")
mainwindow::mainwindow(QWidget *parent)
: QMainWindow(parent) {
//Initialize other stuff
ui.setupUi(this);
//Problem here
connect(ui.board, SIGNAL(Drawspace::mousePressEvent(QMouseEvent*)), this, SLOT(on_click(QMouseEvent*)));
}
void mainwindow::on_click(QMouseEvent * event) {
QMessageBox::information(this, tr("Dialog"), "You clicked the screen");
//Do stuff here
}
It builds fine, and when I click the drawspace, I get a dialog that says "You clicked the board (from QGraphicsView)", but I don't get the second from "You clicked the screen".
And yes, the header file for mainwindow has the Q_OBJECT macro
EDIT: Can be fixed by manually defining your own signal, calling that, and changing the connection to use Qt5 syntax. See my answer for code
OP here, the problem can be fixed by defining a signal, calling emit on it and changing the connection to use Qt5 syntax.
//New drawspace: Added signal definition and called emit on it from mousePressEvent
class Drawspace : public QGraphicsView, public QObject {
Q_OBJECT
public:
Drawspace(QGraphicsScene * scene, QWidget * parent) : QGraphicsView(scene, parent) {}
Drawspace(QWidget* parent) : QGraphicsView(parent) {}
void setObjectName(QString str) { QGraphicsView::setObjectName(str); }
void mousePressEvent(QMouseEvent * event) {
QMessageBox::information(this, tr("Dialog"), "Detected click in Drawspace");
emit clicked(event);
}
Q_SIGNALS:
void clicked(QMouseEvent * event);
};
//Used Qt5 syntax, this will automatically pass arguments
connect(ui.board, &Drawspace::clicked, this, &mainwindow::on_click);
mousePressEvent() is not a signal. It is a member function. You cannot connect() a function to a function like you can connect a signal to a signal.
Qt signals are declared as functions, but they get some special treatment. You'd need to use Qt's keywords to signify a signal so it can get the treatment that will make it work.
Make the mousePressEvent virtual, or you are creating a NEW method:
virtual void mousePressEvent(QMouseEvent * event)
and say it is overriden (depending on your compiler:)
virtual void mousePressEvent(QMouseEvent * event) override (Q_DECL_OVERRIDE)
I have a text editor like program which is a QMainWindow inherited class. There, when I click Find, the connection,
connect(actionFind,SIGNAL(triggered()),this,SLOT(actionFindTriggered()));
Activates. And the defination of that function is
void MainWindow::actionFindTriggered() {
new Find(this);
}
My Find class is
class Find : public QDialog, public Ui::Dialog
{
public:
Find(QWidget *parent=0);
private:
Ui::Dialog *ui;
public slots:
void buttonFindTriggered();
};
And the definition is
Find::Find(QWidget *parent)
: QDialog(parent)
{
ui = new Ui::Dialog;
ui->setupUi(this);
show();
this->
connect(ui->buttonClose, SIGNAL(clicked()), this, SLOT(close()));
connect(ui->buttonFind, SIGNAL(clicked()), this, SLOT(buttonFindTrigddgered()));
}
void Find::buttonFindTriggered() {
qDebug() << "FIND ACTIVATED";
}
What is the problem
When I clicked find from the main window, find window works successfully but could not make the connection. And I get the following msg on console,
Object::connect: No such slot QDialog::buttonFindTriggered() // Edited
Object::connect: (sender name: 'buttonFind')
Object::connect: (receiver name: 'Dialog')
Edited due to a typo...!
You forgot the Q_OBJECT macro.
Also - consider using this notation for getting slot auto-connection (setupUI will automatically connect these slot for you).
void on_buttonFind_clicked();
void on_buttonClose_clicked();
As the error message states, it can't find the slot:
buttonFindTrigddgered()
because it should be:
buttonFindTriggered()
I have two widgets defined as follows
class mainWindow : public QWidget
{
Q_OBJECT
public:
mainWindow();
void readConfig();
private:
SWindow *config;
QVector <QString> filePath;
QVector <QLabel*> alias,procStatus;
QVector <int> delay;
QGridLayout *mainLayout;
QVector<QPushButton*> stopButton,restartButton;
QVector<QProcess*> proc;
QSignalMapper *stateSignalMapper, *stopSignalMapper, *restartSignalMapper;
public slots:
void openSettings();
void startRunning();
void statusChange(int);
void stopProc(int);
void restartProc(int);
void renew();
};
class SWindow : public QWidget
{
Q_OBJECT
public:
SWindow(QWidget *parent=0);
void readConfig();
void addLine(int);
private:
QVector<QPushButton*> selectButton;
QVector<QLabel*> filePath;
QVector<QLineEdit*> alias;
QSignalMapper *selectSignalMapper;
QVector<QSpinBox*> delay;
QGridLayout *mainLayout;
public slots:
void selectFile(int);
void saveFile();
void addLineSlot();
};
when i create and display SWindow object from mainWindow like this
void mainWindow::openSettings()
{
config = new SWindow();
config->show();
}
everything is ok, but now i need to access the mainWindow from SWindow, and
void mainWindow::openSettings()
{
config = new SWindow(this);
config->show();
}
doesn't display SWindow. How can i display SWindow?
How do i call a function on widget close?
By default a QWidget isn't a window. If it is not a window and you specify a parent, it will be displayed inside the parent (so in your case it is probably hidden by other widgets inside your mainWindow).
Look at windowFlags() too. Or you could make your SWindow inherit from QDialog, depending on what you use it for.
As for calling a function on widget close : you could reimplement closeEvent().
When you do config = new SWindow(this); you're setting the parent of config to be the instance of mainWindow.
This means config is no longer a top-level widget, therefore it won't display outside the mainWindow instance (specifically, it would need to be the central widget or inside the mainWindow instance's layout to be displayed).
EDIT: Sorry - I missed your last question; How do i call a function on widget close
You will want to override the QWidget::closeEvent(QCloseEvent *event) method. This gets called when you close a top-level widget. The most practical thing to do is emit() a signal so that another class can handle it having been closed.
As noted by Leiaz, you can use the windowsFlags flag when you create the widget. It would look like this:
void mainWindow::openSettings()
{
config = new SWindow(this, Qt::window);
config->show();
}
To reimplement the closeEvent:
header:
protected:
virtual void closeEvent ( QCloseEvent * event )
cpp:
void sWindow::closeEvent(QCloseEvent *event)
{
this->parentWidget()->SomeFunction();
qWidget::closeEvent(event);
}
However, its probably better to use signal/slots for your case here. Since you said you want to call the parent's renew method on some button click in sWindow, what you want is to EMIT a signal everytime the button is clicked, and connect this signal in the parent with the parent's refresh slot.
void sWindow::sWindow()
{
...
connect(ui.button, SIGNAL(clicked()), this, SLOT(btnClicked()));
}
void sWindow::btnClicked()
{
// whatever else the button is supposed to do
emit buttonClicked();
}
and in your parent class
void mainWindow::openSettings()
{
config = new SWindow(this, Qt::window);
connect(config, SIGNAL(buttonClicked()), this, SLOT(refresh()));
config->show();
}
I have to connect focus event from some QLineEdit element (ui->lineEdit) to the method focus(). How can I do this?
There is no signal emitted when a QLineEdit gets the focus. So the notion of connecting a method to the focus event is not directly appropriate.
If you want to have a focused signal, you will have to derive the QLineEdit class. Here is a sample of how this can be achieved.
In the myLineEdit.h file you have:
class MyLineEdit : public QLineEdit
{
Q_OBJECT
public:
MyLineEdit(QWidget *parent = 0);
~MyLineEdit();
signals:
void focussed(bool hasFocus);
protected:
virtual void focusInEvent(QFocusEvent *e);
virtual void focusOutEvent(QFocusEvent *e);
};
In the myLineEdit.cpp file you have :
MyLineEdit::MyLineEdit(QWidget *parent)
: QLineEdit(parent)
{}
MyLineEdit::~MyLineEdit()
{}
void MyLineEdit::focusInEvent(QFocusEvent *e)
{
QLineEdit::focusInEvent(e);
emit(focussed(true));
}
void MyLineEdit::focusOutEvent(QFocusEvent *e)
{
QLineEdit::focusOutEvent(e);
emit(focussed(false));
}
You can now connect the MyLineEdit::focussed() signal to your focus() method (slot).
I assume you mean connect as in signals/slots, focus event isn't a signal it's a virtual method you have to override in order to change the behavior:
http://doc.qt.io/qt-5/qlineedit.html#focusInEvent