Qt Whenever pressed enter in QTextEdit - c++

Whenever I pressed enter in my QTextEdit it'll perform a click on my login button. Somehow this causes a crash of my QtCreator. How can I change what'll happen If I press enter in my QTextEdit?

You need to subclass QTextEdit and catch the event you're interested in by overriding the appropriate method:
class MyTextEdit : public QTextEdit
{
Q_OBJECT
public:
void MyTextEdit::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Return)
{
login(); // or rather emit submitted() or something along this way
}
else
{
QTextEdit::keyPressEvent(event);
}
}
};
Alternatively, you can install an event filter on the text edit.

Related

Why my data can not transfer between forms with SIGNAl/SLOT?

I have main window and 1 dialog in Qt(in Linux OS).I want to send some thing from main window to my dialogs. When the user press menu button, then my button clicked emit a signals. this is my code in main.cpp:
MainWindow w;
MyDialog m;
//------------------------------
//this connection send key button press mood from MainWindow
QObject::connect(&w,SIGNAL(pressMood(QString)),
&m,SLOT(getPressMood(QString)));
w.show();
and this is my mainwindos.h:
signals:
void pressMood(QString mood) ;
mainwindow.cpp:
void MainWindow::on_btnMenu_clicked()
{
if(database->checkEmpty())
{
menu mn;/*=new menu();*/
mn.showFullScreen();
}
else
{
MyDialog *d=new MyDialog(this);
d->show();
d->raise();
d->activateWindow();
emit pressMood("menu");
if(d->Accepted>0)
{
if(loginResult)
{
menu *mn=new menu();
mn->showFullScreen();
}
}
else
QMessageBox::warning(this, tr("Login failed"), "Sorry.Your authenticate is not valid.", QMessageBox::Ok);
}
}
//--------------------------------------------
void MainWindow::on_btnPassword_clicked()
{
//emit sendID2(result);
CardDialog *d=new CardDialog(this);
emit pressMood("pass");
d->show();
d->raise();
if(d->Accepted<=0)
QMessageBox::warning(this, tr("Login failed"), "Sorry.Your authenticate is not valid.", QMessageBox::Ok);
}
I do not use dialog.exec() because I do not need to show modal.
MyDialog.h:
public slots:
void getPressMood(QString mood);
and MyDialog.cpp:
//=================================================
void MyDialog::getPressMood(QString mood)
{
mood=mood;
//ui->lblMood->setText(mood);;
//ui->lblMood->hide();
}
void MyDialog::on_buttonBox_accepted()
{
//QString mood=ui->lblMood->text();
bool st=database->checkPassword(ui->txtID->text(),ui->txtPass->text(),"3");
int id=(ui->txtID->text()).toInt();
//this user is valid to go to menu page
//s/he is admin
if((st)&&
mood=="menu" &&
database->checkAdmin(id))
{
.......
}
when I trace my code line by line. the emit signal is works , it sends the string data to my slot in another form and also the getpressedmood() slot is also worked. But the global var mood become NULL when the dialog show, also I decide to save data in label. In trace mood I see the string is send but when the dialog shows the label becomes to default value.
I can not find the mistake. Could you help me?
It solved.
my mistake is connect the signal and slot in main.cpp. The answer is :
MyDialog *d=new MyDialog(this);
//should connect here not in main.cpp
QObject::connect(this,SIGNAL(pressMood(QString)),
d,SLOT(getPressMood(QString)));
emit pressMood("menu");
d->show();
d->raise();
d->activateWindow();

Context menu on custom shortcut

I have a QGraphicsView in my MainWindow and I have a custom "grab" function on my canvas (which is the QGraphicsView inside my MainWindow) called when I press both mouse buttons.
So I want to activate my context menu only when I press CTRL + right click and prevent from activating only with a right click. It would be important to keep ActionsContextMenu policy.
Is there a way to do that?
Just check for KeyboardModifier. You can specify which modifier you want. In your case, it is the ControlModifier. Then, override the method mousePressEvent() :
void yourClass::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::RightButton &&
event->modifiers() == Qt::ControlModifier)
{
// call your context menu
}
}
If you want to use this, you'll need to call a QMenu. Just add this function in your class :
void yourClass::showMenu()
{
QMenu contextMenu(this);
QAction action("My Action", this);
contextMenu.addAction(&action);
contextMenu.exec();
}

How to merge KeyReleaseEvent with Button

How to merge KeyReleaseEvent and QPushButton using signal.
I mean whenever user will press enter key button should call some function using SLOT. So what i have to use in the signal?
void mywindow::keyReleaseEvent(QKeyEvent *event)
{
switch(event->key())
{
case Qt::Key_Enter:
connect(button1, SIGNAL(clicked()), this, SLOT(fileNew()));
connect(button2, SIGNAL(clicked()), this, SLOT(file()));
break;
}
}
If I understand your question correctly, you want to click some button when pressing the enter key. You can just call the QAbstractButton::click() function to perform a click.
connect(button1,SIGNAL(clicked()),this,SLOT(fileNew()));
connect(button2,SIGNAL(clicked()),this,SLOT(file())); //do this in your constructor, or somewhere else.. just make sure you only do this once
 
void mywindow::keyReleaseEvent(QKeyEvent *event)
{
switch(event->key())
{
case Qt::Key_Enter:
button1->click();
break;
}
}
There is shortcut property to handle such case.
I recommend to use QAction with shortcut value. There is lost of bonus functionality.

Qt5 C++: Subclassing QSpinBox and keyPressEvent won't work

I want to track when the enter/return key is pressed and then to focus on some other widget but if it's not pressed than spinbox should take normal action instead of anything else. I've subclassed QSpinBox and created protected void keyPressEvent(QKeyEvent *event). Inside it is this code:
void MytSpinBox::keyPressEvent(QKeyEvent *event) {
if (event->key() == Qt::Key_Return) {
qDebug() << "return pressed";
editingFinished();
} else {
qDebug() << "Other key";
event->accept();
}
}
This code should work, but it doesn't. I don't know what's wrong, but when I focus on promoted spinbox and try to change number in it, it just won't work, I only get the debug message printed. If I push enter/return key than it print debug message and focus on some other widget which I set in editingFinished().
What am I doing wrong?
If you need to invoke default processing of the event, you need to call base class implementation:
void MytSpinBox::keyPressEvent(QKeyEvent *event) {
if (event->key() == Qt::Key_Return) {
qDebug() << "return pressed";
editingFinished();
} else {
QSpinBox::keyPressEvent(event);
}
}

Qt Browser stack when dropping link to QMainWindow

I have simple drag and drop functions implemented in QmainWindow the reference taken from here and here
all i want to do is to accept valid url and open QDialog when the url dropped .
but when the url drooped and the QDialog poped up the browser is stocked in the background until i close the QDialog . this is wrong the browser should be free all the time.
here is my code :
void MainWindow::dragMoveEvent(QDragMoveEvent *event)
{
if (event->mimeData()->hasFormat("text/html"))
{
event->acceptProposedAction();
}
else
{
event->ignore();
}
}
void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
// accept just text/uri-list mime format
if (event->mimeData()->hasFormat("text/html"))
{
event->acceptProposedAction();
}
else
{
event->ignore();
}
}
void MainWindow::dragLeaveEvent(QDragLeaveEvent *event)
{
event->accept();
}
void MainWindow::dropEvent(QDropEvent *event)
{
QList<QUrl> urlList;
QString fName;
QStringList pathList;
QFileInfo info;
QString suffix;
if (event->mimeData()->hasFormat("text/html"))
{
urlList = event->mimeData()->urls(); // returns list of QUrls
// if just text was dropped, urlList is empty (size == 0)
if ( urlList.size() > 0) // if at least one QUrl is present in list
{
QString url = urlList.at(0).toString();
event->acceptProposedAction();
openDialog(url); // THIS IS THE FUNCTION THAT I OPEN THE QDIALOG window
}
}
//event->acceptProposedAction();
}
void MainWindow::openDialog(QString& slink)
{
QHash<QString,QVariant> DataMap;
QString link = slink;
DataMap.insert("m_webpage",link);
PublishToDialog* pPublishToDialog = new PublishToDialog(this);
pPublishToDialog->Init(DataMap);
if(pPublishToDialog->exec() != QDialog::Accepted)
{
}
}
when i remove the call to the QDialog , so every thing is working fine .
and the browser doesn't stuck. i even tryed as suggested using signal/slot put again
when i start the QDialog when drop invoked the browser stucked!
What exactly is PublishToDialog? I would assume that it is a custom dialog implementation of yours that inherits QDialog. And given this line:
pPublishToDialog->exec() != QDialog::Accepted
This opens the dialog as a modal dialog. A modal dialog is blocking and will block the execution of the current thread until some action is performed on the dialog. Instead of using a modal dialog, you should use a non-modal dialog. Since I am still not sure if PublishToDialog inherits QDialog or what else, I am just going to assume it is. Here is what you could do:
PublishToDialog* pPublishToDialog = new PublishToDialog(this);
// Make it a non-modal dialog
pPublishDialog->setModal(false);
// Connect it to a slot to handle whenever the user performs some action on it
QObject::connect(pPublishDialog, SIGNAL(finished()), this, SLOT(handleDialogAction());
pPublishDialog.show();
You will have to implement handleDialogAction in your code. At the same time, you may want to make pPublishDialog a class-member as you will need it to access QDialog::reuslt in handleDialogAction.