I have a program that needs to check at startup if a path to project is set. For that I have subclassed QMessageBox, added some custom stuff (like spacer to make dialog wider) and I am calling QFileDialog::getExistingDirectory to get directory.
Thing is, user can click Cancel in QFileDialog. But I want user to be returned to QMessageBox to have another chance to set path or quit program altogether. To achieve that I wrote method loop():
CustomMessageBox::loop()
{
while (true) {
this->exec();
if (this->clickedButton() == setPathButton) {
path = QFileDialog::getExistingDirectory(...);
if (!path.isEmpty()) { break; }
} else if (this->clickedButton() == quitButton) {
break;
}
}
}
Then, I have a method getPath():
CustomMessageBox::getPath()
{
loop();
return path;
}
which I call in main.cpp:
CustomMessageBox box;
QString path = box.getPath();
if (!path.isEmpty()) {
// save path, bla, bla
} else {
exit(EXIT_FAILURE)
}
This works, but is this a good practice? I am specifically asking regarding this while inside which resides method exec().
IMO disadvantages of your decision:
Non-standard usage of dialog class. Put strange loop inside some GUI method and call standard exec() method inside it, when the method can easily be called in a standard way.
Hide exec() call from "user" (another programmer)
Dual, or even triple, purpose of the one getPath() method:
show dialog
flag that the dialog was accepted/rejected via empty - non-empty string
return directory path string
I suggest inherit QDialog (why message box?):
In Degister I put on form standard Push Button setPathButton and Dialog Button Box buttonBox. Then I removed "Ok" button from the box:
#include <QtWidgets/QDialog>
#include "ui_CustomDialog.h"
class CustomDialog : public QDialog
{
Q_OBJECT
public:
CustomDialog(QWidget *parent = nullptr);
QString path() const { return m_path; };
private slots:
void on_setPathButton_clicked();
void on_buttonBox_rejected(); // You can use any pushButton instead: on_closeButton_clicked()
private:
Ui::CustomDialogClass ui;
QString m_path;
};
...
#include "CustomDialog.h"
#include <QFileDialog>
CustomDialog::CustomDialog(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
}
void CustomDialog::on_setPathButton_clicked()
{
m_path.clear();
QString dir = QFileDialog::getExistingDirectory();
if (!dir.isEmpty())
{
m_path = dir;
done(QDialog::Accepted);
}
}
// You can use any pushButton instead: void on_closeButton_clicked()
void CustomDialog::on_buttonBox_rejected()
{
reject();
}
main.cpp
#include "CustomDialog.h"
#include <QtWidgets/QApplication>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
CustomDialog box;
int code = box.exec();
if (code == QDialog::Accepted)
{
qDebug() << box.path();
// save path, bla, bla
}
else
{
exit(EXIT_FAILURE);
}
return a.exec();
}
Related
I was trying out the "echo plugin" from the qt examples and i changed somethings to try out and stumbled upon an error that i cant fix.
here is the original Echo Window class
#include <QWidget>
#include "echointerface.h"
QT_BEGIN_NAMESPACE
class QString;
class QLineEdit;
class QLabel;
class QPushButton;
class QGridLayout;
QT_END_NAMESPACE
//! [0]
class EchoWindow : public QWidget
{
Q_OBJECT
public:
EchoWindow();
private slots:
void sendEcho();
private:
void createGUI();
bool loadPlugin();
EchoInterface *echoInterface; ***********
QLineEdit *lineEdit;
QLabel *label;
QPushButton *button;
QGridLayout *layout;
};
what i changed is the the line i highlighted with the stars. i changed it into A Qlist like this
QList<EchoInterface *>echoInterfaces
also changed the implementation of the function LoadPlugin() From this:-
bool EchoWindow::loadPlugin()
{
QDir pluginsDir(qApp->applicationDirPath());
pluginsDir.cd("plugins");
foreach (QString fileName, pluginsDir.entryList(QDir::Files)) {
QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));
QObject *plugin = pluginLoader.instance();
if (plugin) {
echoInterface = qobject_cast<EchoInterface *>(plugin);
if (echoInterface)
return true;
}
}
return false;
}
To this :-
bool ModifyWindow::loadPlugin()
{
QDir pluginsDir(qApp->applicationDirPath());
pluginsDir.cd("plugins");
foreach (QString fileName, pluginsDir.entryList(QDir::Files)) {
QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));
QObject *plugin = pluginLoader.instance();
if (plugin) {
foreach ( EchoInterface *a, echoInterfaces)
{
a = qobject_cast <EchoInterface *>(plugin);
}
if(!echoInterfaces.contains(NULL))
return true;
}
}
return false;
}
and thanks in advance
If my guess is correct, you'd like to store a collection of interfaces and your question is about how to read it properly.
I do understand that
assumption is the mother of all screw-ups
:) But let's not focus on the reason for doing this and just guess (again) that the EchoInterface is a base class for a variety of some available interfaces.
What's wrong in your code:
bool ModifyWindow::loadPlugin()
{
QDir pluginsDir(qApp->applicationDirPath());
pluginsDir.cd("plugins");
foreach (QString fileName, pluginsDir.entryList(QDir::Files)) {
QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));
QObject *plugin = pluginLoader.instance();
if (plugin) {
foreach ( EchoInterface *a, echoInterfaces)
{
// this is pointless - only a tmp variable 'a' is modified for a while
// just to be reset on the next iteration
a = qobject_cast <EchoInterface *>(plugin);
}
if(!echoInterfaces.contains(NULL)) // use nullptr instead of NULL
return true; // echoInterfaces is always empty, so it doesnt contain null
}
}
return false;
}
The changes you're loking for:
bool EchoWindow::loadPlugin()
{
QDir pluginsDir(QCoreApplication::applicationDirPath());
pluginsDir.cd("plugins");
const QStringList entries = pluginsDir.entryList(QDir::Files);
for (const QString &fileName : entries) {
QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));
QObject *plugin = pluginLoader.instance();
// it's unnecessary to check if plugin == nullptr,
// because in this case qobject_cast returns nullptr
// and we go directly to pluginLoader.unload();
if (EchoInterface * echoIface = qobject_cast<EchoInterface *>(plugin)) {
echoInterfaces.append(echoIface); // populate the collection with a valid pointer
continue; // to avoid the pluginLoader.unload() call which would destroy the echoIface
}
pluginLoader.unload();
}
// Since we ensured that the echoInterfaces can contain only valid pointers,
// no additional checks are necessary — the fact that something is stored in the collection is enough
return !echoInterfaces.isEmpty();
// or
// return echoInterfaces.size() > 0;
// or even just
// return echoInterfaces.size();
}
In case my assumptions were wrong, then we definitely need a more detailed question.
Is it possible to prevent right-click from opening the default context menu on QGraphicsTextItem ? The menu with "Undo, Redo, Cut, Copy, Paste..". On Ubuntu 18.04, that is. I don't know how this behaves on Windows.
I have overridden the mouse press handler to eat right-clicks in my view and tried to do that also in the item class itself. This actually did prevent the menu on Qt 5.10.0, but for some reason not anymore on 5.11.1:
void EditorView::mousePressEvent(QMouseEvent * event)
{
if (event->button() == Qt::RightButton)
{
return;
}
...
doOtherHandlingStuff();
...
}
In the item itself it doesn't have any effect if I do this:
void TextEdit::mousePressEvent(QGraphicsSceneMouseEvent * event)
{
event->ignore();
return;
}
You have to override the contextMenuEvent method of QGraphicsTextItem:
#include <QtWidgets>
class GraphicsTextItem: public QGraphicsTextItem
{
public:
using QGraphicsTextItem::QGraphicsTextItem;
protected:
void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override
{
event->ignore();
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsScene scene;
QGraphicsView w{&scene};
auto it = new GraphicsTextItem("Hello World");
it->setTextInteractionFlags(Qt::TextEditable);
scene.addItem(it);
w.show();
return a.exec();
}
I have QDialogButtonBox buttons on my dialog which are Ok and Cancel button pair. I have implemented accepted() signal to process when 'Ok' button is pressed but I want to abort quitting dialog if the directory path is invalid.
void SettingsDialog::on_buttonBox_accepted()
{
QDir path( ui->lineEditRootPath->text() );
if ( path.exists( ui->lineEditRootPath->text() ) )
{
QSettings settings; // save settings to registry
settings.setValue(ROOT_PATH, ui->lineEditRootPath->text() );
}
else
{
// abort cancelling the dialog here
}
}
Can the dialog quitting be abort from this handler? Do I have to implement the above code in some other signal? Do I have to use simple button to accomplish this instead of QDialogButtonBox?
This issue comes from the dialog template bundled with Qt Creator. When you create an empty dialog with buttons, the .ui file has connections between the button box and and the underlying dialog. They are created behind your back, so to speak:
So, there really is no problem, since the button box doesn't actually accept the dialog. You must accept the dialog, if you don't then the dialog stays open.
The simple fix is to remove the default connection(s).
Other Nitpicks
You should not use the QDir::exists(const QString &) overload - it won't work. You already provided the path to dir's constructor. Simply use exists().
Thus:
void SettingsDialog::on_buttonBox_accepted()
{
QDir path(ui->lineEditRootPath->text());
if (!path.exists()) return;
QSettings settings; // save settings to registry
settings.setValue(ROOT_PATH, ui->lineEditRootPath->text());
accept(); // accepts the dialog, closing it
}
You could also use the static QFileInfo::exists:
void SettingsDialog::on_buttonBox_accepted()
{
if (! QFileInfo.exists(ui->lineEditRootPath->text()) return;
...
}
Finally, it's probably a nice idea to provide some sort of feedback to the user when an input is invalid. In C++11, that's quite easy to do:
#include <QApplication>
#include <QFileInfo>
#include <QDialog>
#include <QDialogButtonBox>
#include <QLineEdit>
#include <QGridLayout>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QDialog dialog;
QLineEdit edit("/");
QDialogButtonBox buttons(QDialogButtonBox::Ok | QDialogButtonBox::Close);
QGridLayout layout(&dialog);
layout.addWidget(&edit, 0, 0);
layout.addWidget(&buttons, 1, 0);
QObject::connect(&buttons, &QDialogButtonBox::accepted, [&]{
if (!QFileInfo::exists(edit.text())) return;
//...
dialog.accept();
});
QObject::connect(&buttons, &QDialogButtonBox::rejected, [&]{ dialog.reject(); });
QObject::connect(&edit, &QLineEdit::textChanged, [&](const QString&){
if (QFileInfo::exists(edit.text()))
edit.setStyleSheet("");
else
edit.setStyleSheet("* { background: red; }");
});
dialog.show();
return a.exec();
}
After some testing, you've realized that the users have a bad tendency to enter paths that might be on disconnected network volumes. When you attempt to check if they exist, it blocks the GUI just so that the OS can politely tell you "umm, nope".
The solution is to perform the check in a worker thread, so that if it blocks, the UI will not be directly affected. If the worker thread blocks, the path editor background will turn yellow. If the path doesn't exist, the background will turn red and the OK button will be disabled.
One bit of code requires some explanation: QObject::connect(&checker, &Checker::exists, &app, [&](...){...}) connects the checker's signal to a lambda in the thread context of the application object. Since checker's signals are emitted in the checker's thread, without the context (&app), the code would be executed in the checker's thread. We definitely don't want that, the GUI changes must be executed in the main thread. The simplest way to do it is to pass one object we surely know lives in the main thread: the application instance. If you don't pass the proper context, e.g. QObject::connect(&checker, &Checker::exists, [&](...){...})), you'll get undefined behavior and a crash.
#include <QApplication>
#include <QFileInfo>
#include <QDialog>
#include <QDialogButtonBox>
#include <QLineEdit>
#include <QPushButton>
#include <QGridLayout>
#include <QThread>
#include <QTimer>
class Thread : public QThread {
using QThread::run; // final
public:
~Thread() { quit(); wait(); }
};
class Checker : public QObject {
Q_OBJECT
public:
Q_SIGNAL void exists(bool, const QString & path);
Q_SLOT void check(const QString & path) { emit exists(QFileInfo::exists(path), path); }
};
int main(int argc, char *argv[])
{
bool pathExists = true;
QApplication app(argc, argv);
QDialog dialog;
QLineEdit edit("/");
QDialogButtonBox buttons(QDialogButtonBox::Ok | QDialogButtonBox::Close);
QGridLayout layout(&dialog);
layout.addWidget(&edit, 0, 0);
layout.addWidget(&buttons, 1, 0);
QTimer checkTimer;
Checker checker;
Thread checkerThread;
checker.moveToThread(&checkerThread);
checkerThread.start();
checkTimer.setInterval(500);
checkTimer.setSingleShot(true);
QObject::connect(&buttons, &QDialogButtonBox::accepted, [&]{
if (!pathExists) return;
//...
dialog.accept();
});
QObject::connect(&buttons, &QDialogButtonBox::rejected, [&]{ dialog.reject(); });
QObject::connect(&edit, &QLineEdit::textChanged, &checker, &Checker::check);
QObject::connect(&edit, &QLineEdit::textChanged, &checkTimer, static_cast<void (QTimer::*)()>(&QTimer::start));
QObject::connect(&checkTimer, &QTimer::timeout, [&]{ edit.setStyleSheet("background: yellow"); });
QObject::connect(&checker, &Checker::exists, &app, [&](bool ok, const QString & path){
if (path != edit.text()) return; // stale result
checkTimer.stop();
edit.setStyleSheet(ok ? "" : "background: red");
buttons.button(QDialogButtonBox::Ok)->setEnabled(ok);
pathExists = ok;
});
dialog.show();
return app.exec();
}
#include "main.moc"
Is it possible to show a QFileDialog where the user can select a file or a directory, either one?
QFileDialog::getOpenFileName() accepts only files, while QFileDialog::getExistingDirectory() is directories-only, but I need to show a file dialog that can accept both a file or a directory (it makes sense for my program). QFileDialog::Options didn't have anything promising.
QFileDialog currently does not support this. I think the main problem for you here is that the FileMode is not a Q_FLAGS and the values are not power of 2, either, and so, you cannot write this to solve this issue.
setFileMode(QFileDialog::Directory|QFileDialog::ExistingFiles)
To solve this, you would need quite a bit of fiddling, e.g.:
Override the open button click operation.
Get the "treeview" indices properly for both files and directories.
My attempt below demonstrates the former, but I did not really go as far as solving the second because that seems to involve some more fiddling with the selection model.
main.cpp
#include <QFileDialog>
#include <QApplication>
#include <QWidget>
#include <QTreeWidget>
#include <QPushButton>
#include <QStringList>
#include <QModelIndex>
#include <QDir>
#include <QDebug>
class FileDialog : public QFileDialog
{
Q_OBJECT
public:
explicit FileDialog(QWidget *parent = Q_NULLPTR)
: QFileDialog(parent)
{
setOption(QFileDialog::DontUseNativeDialog);
setFileMode(QFileDialog::Directory);
// setFileMode(QFileDialog::ExistingFiles);
for (auto *pushButton : findChildren<QPushButton*>()) {
qDebug() << pushButton->text();
if (pushButton->text() == "&Open" || pushButton->text() == "&Choose") {
openButton = pushButton;
break;
}
}
disconnect(openButton, SIGNAL(clicked(bool)));
connect(openButton, &QPushButton::clicked, this, &FileDialog::openClicked);
treeView = findChild<QTreeView*>();
}
QStringList selected() const
{
return selectedFilePaths;
}
public slots:
void openClicked()
{
selectedFilePaths.clear();
qDebug() << treeView->selectionModel()->selection();
for (const auto& modelIndex : treeView->selectionModel()->selectedIndexes()) {
qDebug() << modelIndex.column();
if (modelIndex.column() == 0)
selectedFilePaths.append(directory().absolutePath() + modelIndex.data().toString());
}
emit filesSelected(selectedFilePaths);
hide();
qDebug() << selectedFilePaths;
}
private:
QTreeView *treeView;
QPushButton *openButton;
QStringList selectedFilePaths;
};
#include "main.moc"
int main(int argc, char **argv)
{
QApplication application(argc, argv);
FileDialog fileDialog;
fileDialog.show();
return application.exec();
}
main.pro
TEMPLATE = app
TARGET = main
QT += widgets
CONFIG += c++11
SOURCES += main.cpp
Build and Run
qmake && make && ./main
Quite old question but I think I have a simpler solution than most for the lazy ones.
You can connect the signal currentChanged of QFileDialog with a function that dynamically changes the fileMode.
//header
class my_main_win:public QMainWindow
{
private:
QFileDialog file_dialog;
}
//constructor
my_main_win(QWidget * parent):QMainWindow(parent)
{
connect(&file_dialog,QFileDialog::currentChanged,this,[&](const QString & str)
{
QFileInfo info(str);
if(info.isFile())
file_dialog.setFileMode(QFileDialog::ExistingFile);
else if(info.isDir())
file_dialog.setFileMode(QFileDialog::Directory);
});
}
And then simply call file_dialog as you would.
if(file_dialog.exec()){
QStringList dir_names=file_dialog.selectedFiles():
}
What worked for me was to use:
file_dialog.setProxyModel(nullptr);
as suggested here, or
class FileFilterProxyModel : public QSortFilterProxyModel
{
protected:
virtual bool filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const
{
QFileSystemModel* fileModel = qobject_cast<QFileSystemModel*>(sourceModel());
return (fileModel!=NULL && fileModel->isDir(sourceModel()->index(sourceRow, 0, sourceParent))) || QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
}
};
...
FileFilterProxyModel* proxyModel = new FileFilterProxyModel;
file_dialog.setProxyModel(proxyModel);
as suggested here, or
class FileDialog : public QFileDialog
{
Q_OBJECT
public:
explicit FileDialog(QWidget *parent = Q_NULLPTR)
: QFileDialog(parent)
{
m_btnOpen = NULL;
m_listView = NULL;
m_treeView = NULL;
m_selectedFiles.clear();
this->setOption(QFileDialog::DontUseNativeDialog, true);
this->setFileMode(QFileDialog::Directory);
QList<QPushButton*> btns = this->findChildren<QPushButton*>();
for (int i = 0; i < btns.size(); ++i) {
QString text = btns[i]->text();
if (text.toLower().contains("open") || text.toLower().contains("choose"))
{
m_btnOpen = btns[i];
break;
}
}
if (!m_btnOpen) return;
m_btnOpen->installEventFilter(this);
//connect(m_btnOpen, SIGNAL(changed()), this, SLOT(btnChanged()))
m_btnOpen->disconnect(SIGNAL(clicked()));
connect(m_btnOpen, SIGNAL(clicked()), this, SLOT(chooseClicked()));
m_listView = findChild<QListView*>("listView");
if (m_listView)
m_listView->setSelectionMode(QAbstractItemView::ExtendedSelection);
m_treeView = findChild<QTreeView*>();
if (m_treeView)
m_treeView->setSelectionMode(QAbstractItemView::ExtendedSelection);
}
QStringList selectedFiles()
{
return m_selectedFiles;
}
bool eventFilter( QObject* watched, QEvent* event )
{
QPushButton *btn = qobject_cast<QPushButton*>(watched);
if (btn)
{
if(event->type()==QEvent::EnabledChange) {
if (!btn->isEnabled())
btn->setEnabled(true);
}
}
return QWidget::eventFilter(watched, event);
}
public slots:
void chooseClicked()
{
QModelIndexList indexList = m_listView->selectionModel()->selectedIndexes();
foreach (QModelIndex index, indexList)
{
if (index.column()== 0)
m_selectedFiles.append(this->directory().absolutePath() + "/" + index.data().toString());
}
QDialog::accept();
}
private:
QListView *m_listView;
QTreeView *m_treeView;
QPushButton *m_btnOpen;
QStringList m_selectedFiles;
};
as suggested here. Credits for the original authors and me.
Connect to the currentChanged signal and then set the file mode to the currently selected item (directory or file). This is a Python3 implementation:
class GroupFileObjectDialog(QFileDialog):
def __init__(self, parent):
super().__init__(parent)
self.setOption(QFileDialog.DontUseNativeDialog)
self.setFileMode(QFileDialog.Directory)
self.currentChanged.connect(self._selected)
def _selected(self,name):
if os.path.isdir(name):
self.setFileMode(QFileDialog.Directory)
else:
self.setFileMode(QFileDialog.ExistingFile)
Tested on PyQt 5.14 running on linux / Ubuntu18.04.
I have some forms with input fields that provide warnings to the user when the fields looses focus (if something is wrong of course). To that end I’ve created my own control that inherits from QLineEdit. For this question consider the extremely simple field that will pop a message box whenever focus is lost when there is text in the control. The actual controls have more logic but all are supposed to pop a message box. (Crossposted to qtcentre.org)
class test_line_edit : public QLineEdit
{
Q_OBJECT
public:
test_line_edit(QWidget * parent = 0) : QLineEdit(parent)
{}
~test_line_edit(){}
protected:
virtual void focusOutEvent(QFocusEvent * e)
{
if(!text().isEmpty())
{
QMessageBox msg;
msg.setText(tr("Test"));
msg.exec();
setFocus(Qt::OtherFocusReason);
}
else
{
QLineEdit::focusOutEvent(e);
}
}
};
Now say I have a simple form with just one of these controls and a QPushButton. If the user types in the control and then clicks the button the messagebox pops but the button gets stuck being drawn looking like it’s pressed. How can I make that stop? As best I can tell the QPushButton’s pressed signal is getting fired but that is all I can hear from the button. Since these test_line_edit controls are going to be used everywhere on many different forms I’m hoping there is something I can change inside the test_line_edit class that will fix the problem. Running Qt 4.7.0 on Windows XP using Visual Studio 2010.
I’ve seen this bug and not sure if it’s related since it’s a different platform and I’m not actually holding the button down: http://bugreports.qt-project.org/browse/QTBUG-7901
Here’s a full program that demonstrates the problem (warning: some code generated from the Qt VS plugin)
main.h:
#include <QtGui/QMainWindow>
#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QHeaderView>
#include <QtGui/QMainWindow>
#include <QtGui/QMenuBar>
#include <QtGui/QPushButton>
#include <QtGui/QStatusBar>
#include <QtGui/QToolBar>
#include <QtGui/QVBoxLayout>
#include <QtGui/QWidget>
#include <QLineEdit>
#include <QMessageBox>
class test_line_edit : public QLineEdit
{
Q_OBJECT
public:
test_line_edit(QWidget * parent = 0) : QLineEdit(parent)
{}
~test_line_edit(){}
protected:
virtual void focusOutEvent(QFocusEvent * e)
{
if(!text().isEmpty())
{
QMessageBox msg;
msg.setText(tr("Test"));
msg.exec();
//setFocus(Qt::OtherFocusReason);
}
else
{
QLineEdit::focusOutEvent(e);
}
QLineEdit::focusOutEvent(e);
}
};
QT_BEGIN_NAMESPACE
class Ui_btn_drawing_testClass
{
public:
QWidget *centralWidget;
QVBoxLayout *verticalLayout;
test_line_edit *line_edit;
QPushButton *btn;
QMenuBar *menuBar;
QToolBar *mainToolBar;
QStatusBar *statusBar;
void setupUi(QMainWindow *btn_drawing_testClass)
{
if (btn_drawing_testClass->objectName().isEmpty())
btn_drawing_testClass->setObjectName(QString::fromUtf8("btn_drawing_testClass"));
btn_drawing_testClass->resize(167, 127);
centralWidget = new QWidget(btn_drawing_testClass);
centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
verticalLayout = new QVBoxLayout(centralWidget);
verticalLayout->setSpacing(6);
verticalLayout->setContentsMargins(11, 11, 11, 11);
verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
line_edit = new test_line_edit(centralWidget);
line_edit->setObjectName(QString::fromUtf8("line_edit"));
verticalLayout->addWidget(line_edit);
btn = new QPushButton(centralWidget);
btn->setObjectName(QString::fromUtf8("btn"));
verticalLayout->addWidget(btn);
btn_drawing_testClass->setCentralWidget(centralWidget);
menuBar = new QMenuBar(btn_drawing_testClass);
menuBar->setObjectName(QString::fromUtf8("menuBar"));
menuBar->setGeometry(QRect(0, 0, 167, 20));
btn_drawing_testClass->setMenuBar(menuBar);
mainToolBar = new QToolBar(btn_drawing_testClass);
mainToolBar->setObjectName(QString::fromUtf8("mainToolBar"));
btn_drawing_testClass->addToolBar(Qt::TopToolBarArea, mainToolBar);
statusBar = new QStatusBar(btn_drawing_testClass);
statusBar->setObjectName(QString::fromUtf8("statusBar"));
btn_drawing_testClass->setStatusBar(statusBar);
retranslateUi(btn_drawing_testClass);
QMetaObject::connectSlotsByName(btn_drawing_testClass);
} // setupUi
void retranslateUi(QMainWindow *btn_drawing_testClass)
{
btn_drawing_testClass->setWindowTitle(QApplication::translate("btn_drawing_testClass", "btn_drawing_test", 0, QApplication::UnicodeUTF8));
btn->setText(QApplication::translate("btn_drawing_testClass", "PushButton", 0, QApplication::UnicodeUTF8));
} // retranslateUi
};
namespace Ui {
class btn_drawing_testClass: public Ui_btn_drawing_testClass {};
} // namespace Ui
QT_END_NAMESPACE
class btn_drawing_test : public QMainWindow
{
Q_OBJECT
public:
btn_drawing_test(QWidget *parent = 0, Qt::WFlags flags = 0): QMainWindow(parent, flags)
{
ui.setupUi(this);
}
~btn_drawing_test(){}
private:
Ui::btn_drawing_testClass ui;
protected slots:
void on_btn_clicked()
{
int breakpoint = 5;
}
void on_btn_pressed()
{
int breakpoint = 5;
}
void on_btn_toggled(bool)
{
int breakpoint = 5;
}
};
main.cpp:
#include <QtGui/QApplication>
#include "main.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
btn_drawing_test w;
w.show();
return a.exec();
}
Thank you for your help and please let me know if I can provide any more information.
I've made some changes to your line edit class, see if it solves the problem:
class test_line_edit : public QLineEdit
{
Q_OBJECT
public:
test_line_edit(QWidget * parent = 0) : QLineEdit(parent) {}
~test_line_edit(){}
protected:
virtual void focusOutEvent(QFocusEvent * e)
{
QLineEdit::focusOutEvent(e);
QEvent *event = new QEvent(QEvent::User);
QApplication::postEvent(this, event);
}
virtual void customEvent(QEvent * event)
{
QLineEdit::customEvent(event);
if (event->type()==QEvent::User)
{
QMessageBox msg;
msg.setText(tr("Test"));
msg.exec();
setFocus(Qt::OtherFocusReason);
}
}
};
Instead of showing modal window in the focusOutEvent method, I've posted an event into the event queue. This worked fine on my ubuntu, don't expect any problems on windows either.
hope this helps, regards