Specific shortcuts not working on QT - c++

I have some problems with Qt Shortcuts. I extracted my problem into a simple piece of code I am reporting here.
In my code, I want my EmptyMainWindow::onShortcutActivated() to be executed when I press the CTRL + SHIFT + A combination on the keyboard.
Here is my main.cpp
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
EmptyMainWindow w;
w.show();
return a.exec();
}
Here is my EmptyMainWindow Class
#include "emptymainwindow.h"
#include "ui_emptymainwindow.h"
#include <QShortcut>
#include <QDebug>
EmptyMainWindow::EmptyMainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::EmptyMainWindow)
{
ui->setupUi(this);
QShortcut *shortcut = new QShortcut (QKeySequence
(Qt::CTRL + Qt::SHIFT + Qt::Key_A ), this);
connect (shortcut, SIGNAL(activated()), this, SLOT(onShortcutActivated()));
}
void EmptyMainWindow::onShortcutActivated()
{
qDebug() << "EmptyMainWindow::onShortcutActivated()";
}
Now, my problem is that this code does not work in this way but works properly if the shortcut is CTRL+SHIFT+B or CTRL+SHIFT+C, etc.
Do you have any idea on why this is happening?
PS: I am working on Windows 7 with the visual studio compiler
Thank you

I have found the reason. Thanks to a software called Windows Hotkey Explorer, that gives all the shortcuts that have been registered to the operating system, I have discovered that another program was kind of reserving this shortcut and, thus, it wasn't passed to my application. Do you have any idea on how is it possible to do that on Windows?

Maybe this is an issue with QWERTY and AZERTY keyboard. Because on the both C and B have the same position but not A. Have you try to trigger your shortcut with CTRL+SHIFT+Q ?

Related

Qt MainWindow ignores width, height and title properties

I've created a simple Qt Widgets application using Qt Creator on my Windows 10 machine. I use the ui file and the designer to change properties of my QMainWindow, but somehow the width, height and windowTitle properties have no effect when I set them in the designer. Example:
However, the resulting application looks like this:
Both size and windowTitle are seemingly ignored. I've also tried setting properties from code, like this (but to no avail):
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle("That's some title you've got there");
}
I have also added a layout to the centralWidget but that only had an effect on the child controls, not the actual window itself (and that seems logical).
Normally, it should work.
Can you try if the following minimal example works for you?
#include <QApplication>
#include <QMainWindow>
main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow m;
m.setWindowTitle("TEST");
m.show ();
a.exec();
}

Application frozen by a QMessageBox

I have this small piece of code:
#include <QApplication>
#include <QWidget>
#include <QBasicTimer>
#include <QMessageBox>
class MyWidget:public QWidget{
public:
QBasicTimer timer;
protected:
void timerEvent(QTimerEvent*e){
if(e->timerId()==timer.timerId()){
timer.stop();
QMessageBox::critical(this,"Oups",
"I hope you were not resizing the main window.");
return;
}
QWidget::timerEvent(e);
}
};
int main(int argc,char*argv[]){
QApplication app(argc,argv);
MyWidget w;
w.timer.start(2000,&w);
w.show();
return app.exec();
}
I display a QWidget which displays a QMessageBox after two seconds.
If I am resizing my main window when the popup is displayed, my mouse cursor does not come back to normal (it keeps a "resizing a Window" look) and the interface is completely frozen. I cannot close the popup and I cannot move my mouse over the Taskbar.
The only solution is to navigate with ALT+TAB to Visual studio and stop the debugger.
System (if it matters):
Windows 7 64 bit.
Visual Studio 2013 + Addin
Qt 5.3.0 alpha
My questions:
Is it a known bug?
Am I doing something wrong?
Is there a simple workaround?
According to Digia Support, this is a bug. However, they provide an acceptable workaround.
Just before the QMessageBox::critical we can add a ReleaseCapture(); like this:
#ifdef Q_OS_WIN
ReleaseCapture();
#endif
The behavior goes back to Qt 4.7 though (cf comment from user3183610). The window will snap back to its original size.

mcvs qwt - must construct a QApplication before a QPaintDevice

I would like to ask if you could help me with my app.
I have an application in C++ using Qt (and Qwt) in MCVS 2010. I want to open QDialog window with QwtPlot on button click from Main Window (QMainWindow). Here is some code:
MainWindow.cpp
void MainWindow::on_pushButton_1_clicked ()
{
Dialog_plot dp;
dp.setModal(true);
dp.exec();
}
Dialog_plot.cpp:
#include <qwt_plot.h>
#include <qwt_plot_canvas.h>
#include <qwt_plot_curve.h>
#include "Dialog_plot.h"
Dialog_plot::Dialog_plot(QWidget *parent)
{
plot = new QwtPlot();
//more code...
main.cpp:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
This code is compiling, but when I click on that pushbutton_1 in my application I'm getting error:
Must construct a QApplication before a QPaintDevice
I know that such error was discussed many times and I was reading a lot about it but I can't see solution for my problem.
One more thing I would like to mention - I have similar application with Qwt plot written by somebody else and his application compiles and works without any problems in my MCVS. I was trying to compare Linker/libraries included but it seems to be the same. So I guess there is a problem with my application, I just can't solve it. I really need some help!

Why does this slot get called twice?

My problem is that when I click on an item in a QMenuBar, the corresponding slot gets called twice. I'm using Qt 4.8.1. I'm not using Qt Designer nor the "auto-connection" feature. Here is my code snippet :
#include <iostream>
#include <QWidget>
#include <QMenuBar>
class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget(QWidget *parent = 0) : QWidget(parent)
{
QMenuBar *menu = new QMenuBar(this);
menu->addAction("Click here");
menu->addAction("Or here");
connect(menu, SIGNAL(triggered(QAction*)), this, SLOT(handleAction(QAction*)));
}
public slots:
void handleAction(QAction *action)
{
std::cout << "Triggered" << std::endl;
}
};
And the main function :
#include "main.h"
#include <QApplication>
int main(int argc, char **argv)
{
QApplication app(argc, argv);
MyWidget w;
w.show();
return app.exec();
}
If you compile this (with the MOC file), you'll see that clicking on "Click here" will print "Triggered" once, and "Or here" twice. I don't understand why.
What am I doing wrong ?
Use Qt::UniqueConnection to solve:
connect(menu, SIGNAL(triggered(QAction*)), this, SLOT(handleAction(QAction*)), Qt::UniqueConnection);
http://doc.qt.io/qt-4.8/qt.html#ConnectionType-enum
I get the same incorrect result as you on Windows 7 x64 using Qt 4.8.1. This certainly seems like a bug.
There was a bug reported and fixed for what appears to be the same behavior on Mac OS X. Although it has been closed, there is a single comment on it that they observed this problem on Windows 7.
I think filing a new bug report would be a good idea.

How do I create a simple Qt console application in C++?

I was trying to create a simple console application to try out Qt's XML parser. I started a project in VS2008 and got this template:
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
return a.exec();
}
Since I don't need event processing, I was wondering whether I may get into trouble if I neglect to create a QCoreApplication and running the event loop. The docs state that it's recommended in most cases.
For the sake of curiosity however, I am wondering how could I make some generic task execute on the event loop and then terminate the application. I was unable to google a relevant example.
Here is one simple way you could structure an application if you want an event loop running.
// main.cpp
#include <QtCore>
class Task : public QObject
{
Q_OBJECT
public:
Task(QObject *parent = 0) : QObject(parent) {}
public slots:
void run()
{
// Do processing here
emit finished();
}
signals:
void finished();
};
#include "main.moc"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// Task parented to the application so that it
// will be deleted by the application.
Task *task = new Task(&a);
// This will cause the application to exit when
// the task signals finished.
QObject::connect(task, SIGNAL(finished()), &a, SLOT(quit()));
// This will run the task from the application event loop.
QTimer::singleShot(0, task, SLOT(run()));
return a.exec();
}
Don't forget to add the
CONFIG += console
flag in the qmake .pro file.
For the rest is just using some of Qt classes.
One way I use it is to spawn processes cross-platform.
You don't need the QCoreApplication at all, just include your Qt objects as you would other objects, for example:
#include <QtCore>
int main()
{
QVector<int> a; // Qt object
for (int i=0; i<10; i++)
{
a.append(i);
}
/* manipulate a here */
return 0;
}
I managed to create a simple console "hello world" with QT Creator
used creator 2.4.1 and QT 4.8.0 on windows 7
two ways to do this
Plain C++
do the following
File- new file project
under projects select : other Project
select "Plain C++ Project"
enter project name
5.Targets select Desktop 'tick it'
project managment just click next
you can use c++ commands as normal c++
or
QT Console
File- new file project
under projects select : other Project
select QT Console Application
Targets select Desktop 'tick it'
project managment just click next
add the following lines (all the C++ includes you need)
add "#include 'iostream' "
add "using namespace std; "
after QCoreApplication a(int argc, cghar *argv[])
10 add variables, and your program code..
example: for QT console "hello world"
file - new file project 'project name '
other projects - QT Console Application
Targets select 'Desktop'
project management - next
code:
#include <QtCore/QCoreApplication>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
cout<<" hello world";
return a.exec();
}
ctrl -R to run
compilers used for above MSVC 2010 (QT SDK) , and minGW(QT SDK)
hope this helps someone
As I have just started to use QT recently and also searched the Www for info and examples to get started with simple examples still searching...
You could fire an event into the quit() slot of your application even without connect().
This way, the event-loop does at least one turn and should process the events within your main()-logic:
#include <QCoreApplication>
#include <QTimer>
int main(int argc, char *argv[])
{
QCoreApplication app( argc, argv );
// do your thing, once
QTimer::singleShot( 0, &app, &QCoreApplication::quit );
return app.exec();
}
Don't forget to place CONFIG += console in your .pro-file, or set consoleApplication: true in your .qbs Project.CppApplication.
You can call QCoreApplication::exit(0) to exit with code 0
Had the same problem. found some videos on Youtube.
So here is an even simpler suggestion. This is all the code you need:
#include <QDebug>
int main(int argc, char *argv[])
{
qDebug() <<"Hello World"<< endl;
return 0;
}
The above code comes from
Qt5 Tutorial: Building a simple Console application by
Dominique Thiebaut
http://www.youtube.com/watch?v=1_aF6o6t-J4