Qt - What is meant by those lines of code - c++

In the C++ GUI Programming with Qt 4 book, part of creating a dialog application, there was the following main.cpp file:
#include <QApplication>
#include <QDialog>
#include "ui_gotocelldialog.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Ui::GoToCellDialog ui;
QDialog *dialog = new QDialog;
ui.setupUi(dialog);
dialog->show();
return app.exec();
}
Can you just describe those lines of code?
Ui::GoToCellDialog ui;
QDialog *dialog = new QDialog;
ui.setupUi(dialog);
Thanks.

"ui_gotocelldialog.h" is a file generated automatically based on the file "gotocelldialog.ui" which contains the GUI for the dialog. Ui::GoToCellDialog::setupUi() must be called for the UI to be initialized.

Lets take a look:
Ui::GoToCellDialog ui;
This line creates an instance of GoToCellDialog. As already have been said, this class automatically generated from the gotocelldialog.ui file. The use-case is:
Open qt-designer and make the interface you want.
Save the file (in our case gotocelldialog.ui)
In your .cpp file write #include "ui_gotocelldialog.h"
Now you can use the interface you designed
PROFIT????
Next:
QDialog *dialog = new QDialog;
This line creates new instance of the QDialog class that represents simple modal window (commonly called as dialog). But your window would be empty after this line. You need to place the controls, do you? How you can do this? Lets see:
ui.setupUi(dialog);
This line uses the interface you designed in the qt-designer. It places this interface to the newly created dialog. So you can see all the controls in the window. Pretty easy as for me.

It sets up the ui described by an qt ui xml file hosted inside of a dialog window/qdialog.

Related

How to add "status bar" to QT widget - without using QTDesigner

I am creating QT QEditText widget (C++) dynamically at run time.
I cannot use "static ways" AKA QTDesigner to add / design the widget - it is NOT a form.
I know "status bar" is used as default in "MainWindow".
I want to add same functions in QTextEdit.
The QT doc has an example of C++ code on how to use "status bar" in MainWindow - that is of little help.
I have added this code to the QTextEdit constructor
childStatusBar = new QStatusBar();
childStatusBar->setStatusTip(" Show current copy drag and drop text");
childStatusBar->showNormal();
childStatusBar->showMessage("Status test message");
It compiles and runs, but there is no "status bar".
I do not know what is missing in my code and would
appreciate an assistance in solving this issue.
(Links , references to code examples would be helpful)
Please make sure to
read the post
and replay with facts, not opinions.
Suggestion for alternates are NOT solutions.
Please avoid any format of "ask your friend", RTFM.
Been there, done that.
As far as Mt Higgins is concerned, I am not asking for editing my post - I am not
posting to get a lecture in English grammar or composition.
Cheers and
"... thanks for watching..."
Just create a Qt MainWindow or Widget example and create a statusbar on it. Build the project and you can find your ui_...h in your build directory. Open it and find the QStatusBar widget. You can copy from there.
Note: Layouts are very important in Qt. So don't forget main-widget / main-window main layout and it's relation with statusbar. And also statusbar's layout.
As we can see in QStatusBar document :
Typically, a request for the status bar functionality occurs in
relation to a QMainWindow object. QMainWindow provides a main
application window, with a menu bar, toolbars, dock widgets and a
status bar around a large central widget
So you need something like this code:
#include <QApplication>
#include <QMainWindow>
#include <QStatusBar>
#include <QTextEdit>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow *w = new QMainWindow();
w->resize(600, 400);
QTextEdit *textEdit = new QTextEdit(w);
textEdit->setStatusTip("Show current copy drag and drop text");
auto childStatusBar = new QStatusBar(w);
childStatusBar->showMessage("Status test message");
w->setStatusBar(childStatusBar);
w->setCentralWidget(textEdit);
w->show();
return a.exec();
}
and also QTextEdit has a setStatusTip function that you can add "Show current copy drag and drop text" there instead of childStatusBar.
NOTED: As I mentioned ‍QTextEdit‍ cannot have ‍QStatusBar as you use in your code.
Now, if you don't want to use this method(use QMainWindow ), you have to create a custom and personalized class for yourself.
This means that you have to write the StatusBar by yourself

Non-modal QWidget dialog that stays on top of the window

I want a dialog which stays on top of my main window and not other windows. I derived a class and added some flags. If I call the dialog now with show() the dialog appears and is staying on top as long as I don't press a button or whatever. Then the dialog goes to background again.
Dial::Dial(QWidget *parent) : QWidget(parent)
{
this->setWindowFlags(Qt::Tool | Qt::Dialog);
// ...
Consequently, I looked into the docu and found this:
Indicates that the widget is a tool window. A tool window is often a
small window with a smaller than usual title bar and decoration,
typically used for collections of tool buttons. If there is a parent,
the tool window will always be kept on top of it.
Happily, I added this line into my singleton creating the dialog.
d->mainWindow = new Foo();
d->dial->setParent(d->mainWindow);
Now the dialog is just embedded into my central widget (QOpenGlWidget) and is not a dialog anymore. Somehow, I seem to lack understanding what the docu is telling me? How can I get the dialog stay on top of my application and what does the docu mean?
I'm not able to reproduce your problem. The following code will generate a QWidget that will allways stay on top of the QMainWindow:
#include "QApplication"
#include "QMainWindow"
#include "QLineEdit"
int main(int argc, char * argv[])
{
QApplication a(argc, argv);
QMainWindow w;
w.show ();
QWidget *pLineEdit = new QWidget(&w);
pLineEdit->setWindowFlags(Qt::Tool | Qt::Dialog);
pLineEdit->show ();
a.exec ();
}
Tested with Qt 5.9.
Not sure if you've already solved this by now but you can try the WindowStaysOnTopHint flag when you construct the dialog:
Qt::WindowFlags flags = this->windowFlags();
flags |= Qt::WindowStaysOnTopHint;
this->setWindowFlags(flags);
Then use show() instead of exec() to make it non-modal:
dlg->show();
You need to set the modality (documentation) of the widget, like this:
QWidget *dialog = new QWidget(window, Qt::Dialog);
dialog->setWindowModality(Qt::ApplicationModal);
dialog->show();
However, I'd recommend to use the pre-configured QDialog class, which handles all that stuff for you:
QDialog *dialog = new QDialog(window);
dialog->exec();
Use QDialog instead of QWidget, and pass the parent widget in its constructor function.
QDialog* pDlg = new QDialog(this);
pDlg->show();

QTreeView no show when placed inside QDockWidget

I want to show a file system using QTreeView on a QDockWidget. The tree will be dynamically changed, so I decided to use QTreeView instead of QTreeWidget.
Here is my code:
QFile file(":/default.txt");
file.open(QIODevice::ReadOnly);
TreeModel model(file.readAll());
file.close();
QTreeView w;
w.setModel(&model);
swatch1->setWidget(&w);
w.setEnabled(true);
addDockWidget(leftarea, swatch1);
swatch1 is of type QDockWidget. The above code is inside a function body of type (inherited from) MainWindow. The code runs smoothly, and the tree does not show up.
I also tried another way: putting QTreeView into a QVBoxLayout (using setWidget method), which in turn be put into a QDockWidget (using setLayout method). This 2nd code also runs smoothly, and the tree does not show up.
This code is copied from a working example on Qt Creator IDE, and I tested it working. The only difference is, in the original QTreeView example, the above code is placed inside the main() { ..... } function.
Does anyone has a working example, putting QTreeView into QDockWidget and working (the code actually shows the tree)? Thanks in advance.
I'm not quite sure what went wrong in the OP. However, I made a minimal complete sample to see whether there are pitfalls:
// standard C++ header:
#include <iostream>
#include <string>
// Qt header:
#include <QApplication>
#include <QDockWidget>
#include <QFileSystemModel>
#include <QMainWindow>
#include <QTreeView>
using namespace std;
int main(int argc, char **argv)
{
cout << QT_VERSION_STR << endl;
// main application
#undef qApp // undef macro qApp out of the way
QApplication qApp(argc, argv);
// setup GUI
QMainWindow qWin;
QDockWidget qDock;
qDock.setAllowedAreas(Qt::AllDockWidgetAreas);
QTreeView qTreeView;
QFileSystemModel qFSModel;
qTreeView.setModel(&qFSModel);
QString path = QDir::currentPath();
QModelIndex indexPath = qFSModel.index(path);
qTreeView.scrollTo(indexPath);
qDock.setWidget(&qTreeView);
qWin.addDockWidget(Qt::TopDockWidgetArea, &qDock);
qWin.show();
// run application
return qApp.exec();
}
Compiled and tested it with VS2013, Qt 5.6 on Windows 10 (64 bit):
As can be seen in the snapshot, the QTreeView is visible (docked and undocked). I checked that both re-act on mouse clicks - they did.
(I guess this is one of my most minimal Qt applications I ever wrote.)
Scheff,
Thank you very much for your answer. Sorry I may not be clear about what I am asking: the tree become visible when this code section is in the main() { ....} function:
QFile file(":/default.txt");
file.open(QIODevice::ReadOnly);
TreeModel model(file.readAll());
file.close();
QTreeView w;
w.setModel(&model);
w.show();
But the same code (almost same) does not working (program runs but the tree is not visible) when this section of code is in a class function inside MainWindow and QTreeView is added to a QDockWidget:
QFile file(":/default.txt");
file.open(QIODevice::ReadOnly);
TreeModel model(file.readAll());
file.close();
QTreeView w;
w.setModel(&model);
swatch1->setWidget(&w);
addDockWidget(leftarea, swatch1);
here, leftarea is a Qt:DockWidgetArea, and swatch1 is an object of type inherited from QDockWidget. when run this program, swatch (a QDockWidget) is visible, but not the tree. still struggling ...
The problem solved. The original code I wrote by itself is correct, but it is in an object method, and as soon as the execution leaves the object, the tree is destroyed.
So, it is a C++ variable scoping problem, not exactly a Qt problem. I have been using python for a while, and just switch back to C++.
Scheff, thank you for your posting confirmed to me that the Qt code is correct, and suggests to me that something else is wrong.

QT HTML5 with Menu Bar like real program

I'm working on an QT HTML5 application and I was wondering how i could add an top menu just like normal program ( with the default file, tools, help... options ).
I think that I've to change something in the html5applicationviewer.cpp, but I've got 0 knowledge of that ( I'm learning this... )
Even if you could point me a little bit in the right direction, I'm grateful. I've searched around, but found absolutely nothing about this topic ( but maybe i didn't search right... )
If you need more info, please ask.
Simplest way to add normal "desktop"-style menus to a Qt app is to use QMainWindow which has good support for menus.
Here's something to get you started. First I created the default HTML5 Qt application with Qt Creator (SDK version 5.2.1). Then I edited the main.cpp and added some lines. Result is below, original lines without comment and all added lines with comment.
#include <QApplication>
#include <QMainWindow> // added
#include <QMenuBar> // added
#include <QMenu> // added
#include <QAction> // added
#include "html5applicationviewer.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QMainWindow w; // important, viewer is in stack so w must be before it!
Html5ApplicationViewer viewer;
w.setCentralWidget(&viewer); // set viewer as the central widget
QMenu *fileMenu = w.menuBar()->addMenu("&File"); // create file menu
QAction *exitAction = fileMenu->addAction("&Exit"); // create exit action
QObject::connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit())); // make the action do something
viewer.setOrientation(Html5ApplicationViewer::ScreenOrientationAuto);
//viewer.showExpanded(); // will be shown by main window
viewer.loadFile(QLatin1String("html/index.html"));
w.show(); // show main window
return app.exec();
}

Set QLineEdit focus in Qt

I am having a qt question. I want the QLineEdit widget to have the focus at application startup. Take the following code for example:
#include <QtGui/QApplication>
#include <QtGui/QHBoxLayout>
#include <QtGui/QPushButton>
#include <QtGui/QLineEdit>
#include <QtGui/QFont>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *window = new QWidget();
window->setWindowIcon(QIcon("qtest16.ico"));
window->setWindowTitle("QtTest");
QHBoxLayout *layout = new QHBoxLayout(window);
// Add some widgets.
QLineEdit *line = new QLineEdit();
QPushButton *hello = new QPushButton(window);
hello->setText("Select all");
hello->resize(150, 25);
hello->setFont(QFont("Droid Sans Mono", 12, QFont::Normal));
// Add the widgets to the layout.
layout->addWidget(line);
layout->addWidget(hello);
line->setFocus();
QObject::connect(hello, SIGNAL(clicked()), line, SLOT(selectAll()));
QObject::connect(line, SIGNAL(returnPressed()), line, SLOT(selectAll()));
window->show();
return app.exec();
}
Why does line->setFocus() sets the focus on the line widget #app startup only if it is placed after laying out the widgets and if used before it's not working?
Keyboard focus is related to widget tab order, and the default tab order is based on the order in which widgets are constructed. Therefore, creating more widgets changes the keyboard focus. That is why you must make the QWidget::setFocus call last.
I would consider using a sub-class of QWidget for your main window that overrides the showEvent virtual function and then sets keyboard focus to the lineEdit. This will have the effect of always giving the lineEdit focus when the window is shown.
Another trick that might work is by using the singleshot timer:
QTimer::singleShot(0, line, SLOT(setFocus()));
Effectively, this invokes the setFocus() slot of the QLineEdit instance right after the event system is "free" to do so, i.e. sometime after the widget is completely constructed.
Perhaps this is an update as the last answer was in 2012 and the OP last edited the question in 2014. They way I got this to work was to change the policy and then set the focus.
line->setFocusPolicy(Qt::StrongFocus);
line->setFocus();
In Qt setFocus() is a slot, you can try other overloaded method which takes a Qt::FocusReason parameter like the line shown below:
line->setFocus(Qt::OtherFocusReason);
You can read about focus reason options in the following link:
http://doc.trolltech.com/4.4/qt.html#FocusReason-enum