I am trying to create a MDI document program. I have a question on creating the subwindow.
This is my mainwindow constructor:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle(tr("MDI"));
workspace = new QMdiArea;
setCentralWidget(workspace);
//fileNew();
createActions();
createMenus();
createToolbars();
statusBar()->showMessage(tr("Done"));
enableActions();
}
The interesting point is the fileNew(); function. It is a private slot function actually which I want to invoke when "New File" button is triggered. Here is the private slot fileNew() function:
void MainWindow::fileNew()
{
DocumentWindows* document = new DocumentWindows;
workspace->addSubWindow(document);
}
This function works perfectly when I call from the mainwindow constructor. However, there is a problem when I call it from the createActions(); function which uses a signal-slot mechanism.
Here is my createActions()
void MainWindow::createActions()
{
newAction = new QAction(QIcon(":/Image/NewFile.png"),tr("&New"),this);
newAction->setShortcut(tr("Ctrl+N"));
newAction->setToolTip(tr("Open new document"));
connect(newAction, SIGNAL(triggered(bool)), this, SLOT(fileNew()));
}
No subwindow is created even the SLOT is triggered. Subsequently, I find out that if I add document->show();, everything works well.
void MainWindow::fileNew()
{
DocumentWindows* document = new DocumentWindows;
workspace->addSubWindow(document);
document->show();
}
My question is: Why the show() function is needed in a SLOT but not in the constructor?
PS. DocumentWindows is just a class inherits QTextEdit.
This problem has nothing to do with the class of the widgets one is using. It is unrelated to documents, MDI, or the main window. After you add a child widget to a widget that is already visible, you must explicitly show it. Otherwise, the widget will remain hidden.
All widgets are hidden by default. When you initially show the MainWindow, all of its children are recursively shown too. When you later add a child MDI widget, it remains hidden. When widgets are added to layouts, they are shown by default - but your widget is managed by the MDI area, not by a layout.
This is a minimal test case demonstrating your issue:
// https://github.com/KubaO/stackoverflown/tree/master/questions/widget-show-32534931
#include <QtWidgets>
int main(int argc, char ** argv) {
QApplication app{argc, argv};
QWidget w;
w.setMinimumSize(200, 50);
QLabel visible{"Visible", &w};
w.show();
QLabel invisible{"Invisible", &w};
invisible.move(100, 0);
return app.exec();
}
Related
What I was hoping to do was have a standard QMainWindow class with menus, toolbar, plus various widgets in a layout, one of which would be a graphics view showing a graphics widget that contained a graphics layout. But whenever I put the graphics view into a main window nothing gets displayed. If I create my graphics view with the graphics widget containing a layout inside the main() function, then everything is visible.
As a test I took the working code provided in the Qt Basic Graphics Layouts Example, created a QMainWindow class in main, and moved the QGraphicsScene, Window and QGraphicsView creation to the main window class.
I tested the main window class on its own, and widgets like a line edit show up fine. But the code below, taken from main in the example, no longer works when in the main window class.
QGraphicsScene scene;
Window *window = new Window;
scene.addItem(window);
QGraphicsView view(&scene);
view.resize(600, 600);
view.show();
I just get a blank area. If I don't add the Window widget, but instead, for example, draw an ellipse then that is visible. If I add a plain QGraphicsWidget with a background colour then that is also visible. It is just when things are inside a layout on a graphics widget that I get nothing. I've been searching for answers, digging into the documentation and even looking through the Qt source to see if I can figure out if what I am trying to do is even possible, but without any luck.
My main window:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QGraphicsScene scene;
Window *window = new Window;
window->resize(600, 600);
scene.addItem(window);
QGraphicsView view(&scene);
view.resize(600, 600);
view.show();
setCentralWidget(&view);
resize(600,600);
}
Code in main:
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow w;
w.show();
return app.exec();
}
One problem is here:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
[...]
QGraphicsView view(&scene);
view.resize(600, 600);
view.show();
[...]
}
You've declared the view widget on the stack, which means it will be automatically destroyed as soon as your MainWindow constructor returns, and thus the user will never see it. What you should do instead is something like this:
QGraphicsView * view = new QGraphicsView(&scene, this);
view->resize(600, 600);
view->show();
You'll have a similar problem with your QGraphicsScene object:
QGraphicsScene scene;
... since it is also declared as a local variable inside the constructor-method, it will also be destroyed when the constructor-method returns, leaving you with no scene. I suggest making it a class-member variable instead; that way it will last as long as your MainWindow does.
(Note that declaring items on the stack worked in the example-program you copied from, only because they were declared in the directly in the main() method, and main() doesn't return until the program is ready to exit... thus the objects in the example program would not be destroyed until the program was exiting anyway)
I have a MainWindow with a menu that opens a dialog for registration. How can I update the tableView in the MainWindow after the registration?
Here is my MainWindow implementation:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){
ui->setupUi(this);
}
void MainWindow::list()
{
qDebug() << "test";
QSqlQueryModel *model = new QSqlQueryModel();
//model->clear();
model->setQuery("SELECT test_qt FROM db_qt WHERE strftime('%Y-%m-%d', date)='"+dateTime.toString("yyyy-MM-dd")+"'");
model->setHeaderData(0, Qt::Horizontal, tr("qt_test"));
ui->tableView->setModel(model);
}
void MainWindow::on_actionMenu_triggered()
{
dialog_test->show();
}
Here is my Dialog implementation
Dialog_test::Dialog_test(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog_test)
{
ui->setupUi(this);
}
void Dialog_test::insert_date(){
QSqlQuery qry;
qry.prepare("INSERT INTO db_qt(test_qt) VALUES (?)");
qry.addBindValue(id);
if (qry.lastInsertId()>0){
QMessageBox::information(this,"test", "Success");
MainWindow *mw = new MainWindow(this);
mw->list(); // I call back list, but not update the tableView the MainWindow.
}
}
The following line in your code
MainWindow *mw = new MainWindow(this);
creates a new main window and updates the list of it. I assume this actually happens, but the window is never shown so you do not see any of it. What you actually want to do is update the list of your existing main window.
There are basically two ways of doing that. You can either obtain a pointer to the existing main window (which can be provided to the constructor of the dialog or a method of its own) or use the Signals and Slots concept of Qt which is the way to go in my opinion.
First of all, you define the signal in the header of the dialog:
...
signals:
void user_registered();
...
Then you emit the signal in your function
//MainWindow *mw = new MainWindow(this);
//mw->list();
emit this->user_registered();
Make sure the list() method is declared as a SLOT in the MainWindow header
Connect the signal in the MainWindow constructor to call the list() slot:
...
QObject::connect(this->dialog_test, SIGNAL(user_registered()), this, SLOT(list()));
...
With this approach, the dialog does not need to know the main window at all. It basically just tells anyone who is interested that a user registered and the main window acts on it completly by itself.
Summary: I wish to use X11 to paint a custom QWidget. It works unless that widget is in a layout or QMainWindow
I have a custom widget derived from QWidget that I'd like to be the main widget in a QMainWindow. When I run something like this:
int main(int argc, char** argv) {
QApplication app(argc, argv);
ModelWidget mw;
mw.show();
return app.exec();
}
everything works fine, including resizing, obscuring and revealing the window contents, etc.
However, if I try to use that widget as the central widget in a QMainWindow, nothing is painted in the central widget area of the QMainWindow.
Here's the constructor of the QMainWindow:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
modelwidg = new ModelWidget;
setCentralWidget(modelwidg);
createActions();
createMenus();
}
I get the feeling that there's something related to the size or resize policy of my custom widget that I need to implement, but I can't find any documentation regarding what functions must be provided by a widget for it to be usable as a central widget in a QMainWindow. What am I missing?
Edit: Here's the custom widget
ModelWidget::ModelWidget(QWidget *parent) :
QWidget(parent)
{
setAttribute(Qt::WA_PaintOnScreen);
setAttribute(Qt::WA_NoSystemBackground);
setAttribute(Qt::WA_OpaquePaintEvent);
setAttribute(Qt::WA_NativeWindow);
setAutoFillBackground(true);
const QX11Info &info = x11Info();
// Elided X11 and glX specific stuff
create(wnd, true, true);
}
void ModelWidget::paintEvent(QPaintEvent *)
{
// scene.render is just some OpenGL stuff
scene->render();
glXSwapBuffers(dpy, glxwnd);
}
void ModelWidget::resizeEvent(QResizeEvent * e)
{
glViewport(0, 0, e->size().width(), e->size().height());
scene->set_aspect(float(e->size().width()) / float(e->size().height()));
update();
}
QSize ModelWidget::sizeHint() const
{
return QSize(640, 480);
}
Further...
According to the docs,
To render outside of Qt's paint system, e.g., if you require native
painting primitives, you need to reimplement QWidget::paintEngine() to
return 0 and set [Qt::WA_PaintOnScreen].
I've done that, but the window still remains unpainted. I suspect that one of my X11 objects, Window, Display*, is being altered by adding this widget to a layout or MainWindow.
I want to open a Widget from my MainWindow. I thought this was easy to do, and all the tutorials I read do it like this:
void MainWindow::on_pushButton_Types_clicked()
{
m_typesWin = new TypesWindow(m_db, this);
m_typesWin->show();
this->hide();
}
However, this only works for me if I don't pass "this" into the constructor. When I add "this" to the constructor, I don't see the widget, the program just stops. If I don't hide "this", then I can see that parts of my widget are actually in my main window. I have no idea why.
EDIT: The classes are automatically created by QtCreator, so they should be alright.
If you want a QWidget to be displayed as a window, a parent widget should not be specified to that widget. Here, because you specify main window as the parent of TypesWindow, TypesWindow becomes embedded in main window. So when you hide main window, TypesWindow embedded in main window also gets hidden.
Since you want TypesWindow to be a separate window, don't pass parent widget to the QWidget constructor in TypesWindow constructor. If you want to access main window from TypesWindow, you can store main window pointer in a pointer field in TypesWindow.
To open a Mainwindows from the new Qwidget:
1)in the NEWWIDGET.CPP:
QWidget *w;
NEWWIDGET::NEWWIDGET(QWidget *parent,QWidget *win) :
QWidget(parent),
ui(new Ui::NEWWIDGET)
{
ui->setupUi(this);
w=win;
}
..
void NEWWIDGET::on_pushButton_clicked()
{
this->hide();
w->show();
}
2)In the NEWWIDGET.H
public:
explicit NEWWIDGET(QWidget *parent=nullptr,QWidget *win=nullptr);
~NEWWIDGET();
I've created an Application which derives from QWidget. When I create an QLabel and yield the show command, it opens in a separate window. I was deriving my BaseClass from QMainWindow before which worked fine.
#include "widget.h"
#include "ui_widget.h"
#include <iostream>
#include <QDebug>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
testlabel = new my_qlabel(parent);
QImage myImage = QImage(QString::fromStdString("D:/Lighthouse.jpg"));
testlabel->setParent(parent);
testlabel->name="testName";
testlabel->setPixmap(QPixmap::fromImage(myImage, Qt::AutoColor));
testlabel->setGeometry(QRect(500, 500, 100, 100));
testlabel->show();
std::cout<<"i am in the output "<<std::endl;
qDebug() << QString("init");
}
Widget::~Widget()
{
delete ui;
}
testlabel = new my_qlabel(parent);
The above should probably instead be
testlabel = new my_qlabel(this);
Also make sure that your my_qlabel constructor is passing its QWidget pointer argument up to the superclass's (QLabel?) constructor. If you forgot to do that, then the my_qlabel object will not have a parent widget, which will cause it to show up as top-level window, which would match the behavior you are seeing.
testlabel->show();
Once you have testlabel's parenting problems fixed, this show() command should no longer be necessary (or appropriate), since any child widgets you add to your Widget object will be automatically show()'n when the Widget itself is first show()'n. (The only time you would need to manually call show() is if you had previously called hide() or setVisible(false) on that same widget, and now you wanted to make it re-appear; or if you had added the child widget to its parent widget after the parent widget was already visible on-screen; neither is the case here)