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();
}
Related
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
I need to implement custom completer, similar to QCompleter but containing customized widgets. I got stuck at the very beginning, I can't make the popup work as it should work. In the following example I attach a completer (well, just a plain widget) to the first line edit. The second line edit is there just to test the focus in/out behavior. The problem is that when the popup displays, it steals the focus from the line edit. But this is not what I want, I want to be able to keep typing in the line edit. I tried several other options as commented out in the code below, but non of them worked.
#include <QApplication>
#include <QLineEdit>
#include <QHBoxLayout>
#include <QWidget>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget container;
QLineEdit editor;
QLineEdit editor2;
QHBoxLayout layout(&container);
layout.addWidget(&editor);
layout.addWidget(&editor2);
QWidget completer; // or QWidget completer(&editor);?
//completer.setFocusProxy(&editor); // tried this, but it does not help
completer.setWindowFlags(Qt::Popup);
// the following lines are an alternative to Qt::Popup but the window
// does not close automatically, which is a problem
// e.g. when moving or resizing the parent window
//completer.setAttribute(Qt::WA_ShowWithoutActivating);
//completer.setWindowFlags(Qt::Tool | Qt::FramelessWindowHint);
QObject::connect(&editor, &QLineEdit::textEdited,
[&] {
completer.resize(editor.width(), 100);
completer.move(editor.mapToGlobal(QPoint(0, editor.height())));
completer.show();
// editor.setFocus(); // does not help either
});
container.show();
return a.exec();
}
How to implement custom completer?
UPDATE: From the docs, I read that "A popup widget is a special top-level widget that sets the Qt::WType_Popup widget flag, e.g. the QMenu widget. When the application opens a popup widget, all events are sent to the popup. Normal widgets and modal widgets cannot be accessed before the popup widget is closed."
So it seems that when I open a popup it automatically receives the events and therefore I have to forward the events to the line edit using QCoreApplication::sendEvent(editor, event); inside my Completer::keyPressEvent. This seems to work fine except the fact that the cursor in the line edit is not visible or not blinking.
Just apply this method to your completer widget:
setWindowFlags(Qt::Tool | Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);
It works for me.
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.
I have an embedded touch screen running linux and my app is Qt/C++.
I start the app with the function main which looks like this:
#include <QApplication>
#include <QThread>
#include <stdlib.h>
int main(int argc, char *argv[])
{
// Create the app
QApplication app(argc, argv);
// Hide the pointer
app.setOverrideCursor( QCursor( Qt::BlankCursor ) );
// Create main window
MainWindow *window = new MainWindow;
// Set up the UI and execute the App
window->show();
app.exec();
// tidy up
delete(window);
return 0;
}
The problem that I have is that when I run the program the cursor is showing. But as soon as I touch the screen (the app is a full-screen app) the cursor then disappears.
I have read that it may be a simple focus issue - i.e. that my app is not in focus or some-such. How can I make sure that my app is the focus at startup?
Or
Is there anything I am doing wrong to hide the mouse pointer?
---EDIT---
If I remove the line app.setOverrideCursor( QCursor( Qt::BlankCursor ) ); then I have focus on my app. So this line somehow makes my app lose focus.
So as a quick hack I put a QTimer in the MainWindow and set it for 100ms (just so that it will only get handled once the event processing starts - i.e. after app.execute()). Then I connected the timer event to a slot function that I call "getFocusOnMeNow()" which in which I have:
this->activateWindow();
this->setFocus();
And this works.
So now I would like to tidy this up. What function/slot can I call AFTER app.execute() within the MainWindow to get focus? I don't see a MainWindow::start() slot that I can overload...?
try
app.setCursorVisible(false);
may this help u.
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.