Custom context menu connecting in Qt - qt

I have a problem with connecting custom menu in QListWidget, the connect function returns false. Here is the code:
I've got some main window class called MainWindow. In its constructor I have this line
connect(ui->notesWidget, SIGNAL(customContextMenuRequested(QPoint &)),
this, SLOT(contextMenuforNotesArea(QPoint &)));
where notesWidget is mentioned QListWidget.
ContextMenuforNotesArea(QPoint &) is defined like this
class MainWindow : public QMainWindow
{
public slots:
void contextMenuforNotesArea(QPoint &pos);
};
void MainWindow::contextMenuforNotesArea(const QPoint &pos){
QMessageBox::information(this, "a", "the function has been finally called");
QMenu contextMenu(tr("Context menu"), this);
contextMenu.addAction(new QAction(tr("Hello"), this));
contextMenu.exec(mapToGlobal(pos));
}
I have also changed the property contextMenu in the listWidget to customContextMenu through form designer.

Change your SLOT signature as follows in the header file
void contextMenuforNotesArea(const QPoint &pos);
Also make sure, when you are doing the connection in the constructor, you do it after the calling of setupUi

Related

Qt connect mainwindow and dialog using signal and slot

I am trying to connect mainwindow and dialog using signal and slot. I am very new to qt. I have a lineEdit and a pushButton in mainwindow.ui, a lineEdit in dialog.ui. And I have those very basic code:
mainwindow.h:
signals:
void sendString(QString);
mainwindow.cpp:
void MainWindow::on_pushButton_clicked()
{
Dialog *mDialog = new Dialog(this);
emit sendString(ui->lineEdit->text());
connect(this, SIGNAL(sendString(QString)), mDialog, SLOT(showString(QString)));
mDialog->show();
}
dialog.h:
private slots:
void showString(QString);
dialog.cpp:
void Dialog::showString(QString str)
{
ui->lineEdit->setText(str);
}
But after I clicked the pushButton, the dialog showed, but nothing changed in the lineEdit.I hope I explain this clearly enough?
Can someone explain to me why and how to solve this? Thanks.
emit signal after connect
void MainWindow::on_pushButton_clicked()
{
Dialog *mDialog = new Dialog(this);
connect(this, SIGNAL(sendString(QString)), mDialog, SLOT(showString(QString)));
mDialog->show();
emit sendString(ui->lineEdit->text());
}
You have to create the connection before the emit.
But in your case you dont need the signal of the of the mainwindow at all. You invoke the showString method directly.

Toggle A QAction To Set QStackedWidget's Current Widget

So what I am trying to do is when I press the showMenu (QAction), the container (QStackedWidget) changes the current widget to menuWidget AND when I press it again it hides.
Ok so I have managed to get this code:
connect(showMenu, SIGNAL(triggered()), map, SLOT(map()));
map->setMapping(menuWidget, container);
Object::connect(map, SIGNAL(mapped(QWidget *)), container, SLOT(setCurrentWidget(QWidget *)));
also if I run:
container->setCurrentWidget(menuWidget);
directly, it works fine, so I have not messed up in that way.
You should create a slot in your class where you show/hide menuWidget.
If you are using a checkable QAction object, then you can use QAction::toggled(bool checked) signal, and use the checked variable to determine if you should show or hide your widget.
If you're not using a checkable QAction object, then you should create a class member variable of type bool that you toggle in your slot:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
...
private:
bool toggleStatus; // set this to false in your constructor
...
};
 
void MainWindow::menuToggled()
{
toggleStatus = !toggleStatus;
if(toggleStatus)
{
container->setCurrentWidget(menuWidget);
}
else
{
container->setCurrentWidget(mdiContainer);
}
}

QDialog with label that I'm trying to paint

from my main window I am launching a dialog which has a label, in which I am trying to paint.
So, the dialog's header file (.h) has two classes, one for the dialog itself and one for my label. So, my label's class is this:
class MyImage : public QLabel
{
Q_OBJECT
public:
explicit MyImage(QWidget *parent = 0);
protected:
void paintEvent(QPaintEvent *e);
};
and in the .cpp, along with the constructor of my QDialog I have the constructor of my MyImage class and the paintEvent function:
MyImage::MyImage(QWidget *parent)
: QLabel(parent)
{
/*...*/
}
void MyImage::paintEvent(QPaintEvent *e)
{
QLabel::paintEvent(e);
QPainter painter(image_label);
painter.setPen(QPen(QBrush(QColor(0,0,0,180)),1,Qt::DashLine));
painter.setBrush(QBrush(QColor(255,255,255,120)));
painter.drawRect(selectionRect);
}
The image_label is a MyImage object. On the constructor of my QDialog I do the following so as to add it to my QDialog's layout:
mainLayout->addWidget(image_label);
But it is null. I get an error message on output (cannot add null widget) and when I try to add a pixmap to the image_label the program crashes.
Thanks in advance for any answers!
void MyImage::paintEvent(QPaintEvent *e)
{
// QPainter painter(image_label); <- Only paint onto yourself.
QPainter painter(this);
painter.setPen(QPen(QBrush(QColor(0,0,0,180)),1,Qt::DashLine));
painter.setBrush(QBrush(QColor(255,255,255,120)));
painter.drawRect(selectionRect);
}
Do not call the base class as any output will be overwritten by the new QPainter. It is crashing because image_label is null.

selecting the complete default text by a mouse click

I have a QLineEdit widget in my menubar which shows the text "search by ID" by default. How can I implement a MouseClicked event handler for the QLineEdit, such that when I click on the LineEdit widget, the default text is cleared and user can enter the text that he wants to search?.
so far
#ifndef SEARCH_H
#define SEARCH_H
#include<QLineEdit>
class search : public QLineEdit
{
signals:
void clicked();
protected:
void mousePressEvent(QMouseEvent *);
};
#endif
You just need to connect QLineEdit::mousePressEvent ( QMouseEvent * e ) signal with function. When this signal will be emited, clear QLineEdit in your function. Simply, isn't it?
EDIT
Or if u have
void mousePressEvent(QMouseEvent *);
in your widget, all what you need is write definition for that method. When user press mouse over QLineEdit, this function will be invoked. Like:
void search::mousePressEvent(QMouseEvent *e)
{
myQLineEdit->setText("");
}
EDIT 2
Then try to do it this way:
class YourWidget : public QLineEdit
{
Q_OBJECT
protected:
void focusInEvent(QFocusEvent* e);
};
and
void YourWidget::focusInEvent(QFocusEvent* e)
{
if (e->reason() == Qt::MouseFocusReason)
{
myQLineEdit->setText("");
}
// You might also call the parent method.
QLineEdit::focusInEvent(e);
}
You'll want to use the QLineEdit::placeholderText property. It shows a grey text which disappears when the user starts editing it (i.e. when it gains focus).
QLineEdit * edit = new QLineEdit;
edit->setPlaceholderText("Search by ID");

How to pass data from one form to another in Qt?

How can I pass data from one form to another in Qt?
I have created a QWidgetProgect -> QtGuiApplication, I have two forms currently. Now I want to pass data from one form to another.
How can I achieve that ?
Thanks.
Here are some options that you might want to try:
If one form owns the other, you can just make a method in the other and call it
You can use Qt's Signals and slots mechanism, make a signal in the form with the textbox, and connect it to a slot you make in the other form (you could also connect it with the textbox's textChanged or textEdited signal)
Example with Signals and Slots:
Let's assume that you have two windows: FirstForm and SecondForm. FirstForm has a QLineEdit on its UI, named myTextEdit and SecondForm has a QListWidget on its UI, named myListWidget.
I'm also assuming that you create both of the windows in the main() function of your application.
firstform.h:
class FistForm : public QMainWindow
{
...
private slots:
void onTextBoxReturnPressed();
signals:
void newTextEntered(const QString &text);
};
firstform.cpp
// Constructor:
FistForm::FirstForm()
{
// Connecting the textbox's returnPressed() signal so that
// we can react to it
connect(ui->myTextEdit, SIGNAL(returnPressed),
this, SIGNAL(onTextBoxReturnPressed()));
}
void FirstForm::onTextBoxReturnPressed()
{
// Emitting a signal with the new text
emit this->newTextEntered(ui->myTextEdit->text());
}
secondform.h
class SecondForm : public QMainWindow
{
...
public slots:
void onNewTextEntered(const QString &text);
};
secondform.cpp
void SecondForm::onNewTextEntered(const QString &text)
{
// Adding a new item to the list widget
ui->myListWidget->addItem(text);
}
main.cpp
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
// Instantiating the forms
FirstForm first;
SecondForm second;
// Connecting the signal we created in the first form
// with the slot created in the second form
QObject::connect(&first, SIGNAL(newTextEntered(const QString&)),
&second, SLOT(onNewTextEntered(const QString&)));
// Showing them
first.show();
second.show();
return app.exec();
}
You could also use pointers to access the QTextEdit (assuming that's what you're using) from the other form.
Following from Venemo's example (where FirstForm has the QTextEdit and SecondForm's the one you need to access the QTextEdit from):
firstform.h:
class FistForm : public QMainWindow
{
...
public:
QTextEdit* textEdit();
};
firstform.cpp:
QTextEdit* FirstForm::textEdit()
{
return ui->myTextEdit;
}
You can then access the QTextEdit's text in SecondForm with something like this (assuming your instance of FirstForm is called firstForm):
void SecondForm::processText()
{
QString text = firstForm->textEdit()->toPlainText();
// do something with the text
}

Resources