When I set mainwindow to fullscreen() , title bar disappears, thats what I want. But when a dialog is opened main window title bar appears again, which is undesirable in my case.I have tried setting several Qt::windowflags but they dont work.Any help will be greatly appreciated.
Quick answer here, you have to do something like this
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyMainWindow window;
window.setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
window.show();
}
PS: What I gather from the net is that, the results are a bit ambiguous. Do let us know the result
To move the dialog in the centre:
1.Calling widget should pass the centre co-ordinates to the dialog window.
I have done it by passing co-ordinates by calling a dialog funuction.
In widget.cpp:
dialog->centre(this->width()/2,this->height()/2);
2.In the dialog.cpp :
`centre(int x,int y)
{
width =x; //store in some global variable
height=y;
}`
3.In the show event of dialog.cpp:
this->move(width,height);
Done. It will place the dialog in the centre of the widget.
Related
I tried launching my window this way:
#include "stdafx.h"
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
setWindowOpacity(0);
QGraphicsOpacityEffect* eff = new QGraphicsOpacityEffect(this);
QPropertyAnimation* ani = new QPropertyAnimation(eff, "windowOpacity");
ani->setDuration(3000);
ani->setStartValue(0);
ani->setEndValue(1);
ani->setEasingCurve(QEasingCurve::OutBack);
ani->start(QPropertyAnimation::DeleteWhenStopped);
}
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
But the window never became visible, I would like to it be initialized with a fade in effect.
What's the proper way to achieve it?
Two issues I see. First, as originally written, your main function is going to exit immediately after opening the window. Add return a.exec(); at the end of main.
Next, you are animating QGraphicsOpacityEffect. As written, your example code has no connection between QGraphicsOpacityEffect and the window. Your animation is animating the property on the effect class, but that doesn't propagate back to the widget. There is no need to use QGraphicsOpacityEffect. Instead, just animate the widget directly:
ui.setupUi(this);
setWindowOpacity(0);
// Notice that the first argument passed in is 'this' - the widget.
// the widget is what you want to animate.
QPropertyAnimation* ani = new QPropertyAnimation(this, "windowOpacity");
ani->setDuration(3000);
ani->setStartValue(0);
ani->setEndValue(1);
ani->setEasingCurve(QEasingCurve::OutBack);
ani->start(QPropertyAnimation::DeleteWhenStopped);
I'm trying to display a Q3DScatter graph in a Main Window designed with Qt Designer so I've added a widget to the main window but I don't know how to embed the graph object in this widget. I tried to create a widget "container" programmatically and embed the graph in it and then putting the widget in a QHBoxLayout (which has been added to the main window using Qt Designer) using the following code in my main window's constructor:
...
Q3DScatter *graph = new Q3DScatter();
QWidget *container = QWidget::createWindowContainer(graph);
ui->horizontal_layout->addWidget(container, 1);
But the window appears to be empty when executing. I'd really appreciate some help.
EDIT: Here's the full code of my main window constructor:
ResultsWindow::ResultsWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::ResultsWindow)
{
ui->setupUi(this);
Q3DScatter *graph = new Q3DScatter();
QWidget *container = QWidget::createWindowContainer(graph);
ui->horizontal_layout->addWidget(container, 1);
}
And here's the main code:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ResultsWindow w;
w.show();
return a.exec();
}
EDIT 2: I forgot to specify that the horizontal layout is embedded in a GridLayout. I tried to create a new project and my code actually works perfectly when I add a horizontal layout directly to the main window. So could the problem be due to the GridLayout?
Code below
#include <QtWidgets>
#include <QGLWidget>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow w;
w.setCentralWidget(new QGLWidget(&w)); // w.setCentralWidget(new QWidget(&w));
QTreeWidget* tree = new QTreeWidget(&w);
QTreeWidgetItem* item0 = new QTreeWidgetItem(tree, QStringList("a"));
QTreeWidgetItem* item1 = new QTreeWidgetItem(tree, QStringList("b"));
tree->setContextMenuPolicy(Qt::CustomContextMenu);
QObject::connect(tree, &QTreeView::customContextMenuRequested, [](){
QMenu menu;
menu.addAction("a");
menu.exec(QCursor::pos());
});
QDockWidget* dock = new QDockWidget("Tree", &w);
dock->setWidget(tree);
w.addDockWidget(Qt::LeftDockWidgetArea, dock);
w.show();
return a.exec();
}
Compile and run it. It requires 2 right clicks to switch context menus among items. However, if I change QGLWidget to QWidget. It is fine. 1 right click can switch context menus among items. Any bugs??? Thanks a lot.
Some observations:
Switch the positions of the tree and the GL widget is ok, i.e. set the tree as the central widget and GL Widget as the one in the dock.
It seems the right click event goes to the central widget and then is blocked by the GL widget.
There is a Qt application. GL-window created into this application by calling XCreateWindow function and I can't edit it.
I need put Xwindow in QWidget inside my Qt applications.
In the documentation:
void QWidget::create ( WId window = 0, bool initializeWindow = true,
bool destroyOldWindow = true ) [protected]
Creates a new widget window if the window is 0, otherwise sets the widget ' s window to window.Initializes the window sets the geometry etc.) if initializeWindow is true. If initializeWindow is false, no initialization is performed. This parameter only makes sense if window is a valid window.
...
For verifying the result of function QWidget::create there is the following code:
class ParentWindow : public QWidget
{
Q_OBJECT
public:
ParentWindow(WId id)
{
create(id);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPushButton* button = new QPushButton("MEGA BUTTON");
button->show();
ParentWindow w(button->winId());
w.show();
return a.exec();
}
When application start a single blank window appears. Although expected window containing a button (or to be a button).
How can I put X11 window into my QWidget?
The problem was resolved:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Display* display = XOpenDisplay(NULL);
XSynchronize(display, True);
XSetErrorHandler(myErrorHandler);
Window x11root = XDefaultRootWindow(display);
int x = 500;
int y = 500;
unsigned int width = 150;
unsigned int height = 150;
unsigned int borderWidth = 0;
long colorBlue = 0xff0000ff;
Window x11w = XCreateSimpleWindow(display, x11root, x, y,
width, height, borderWidth, 1 /*magic number*/, colorBlue);
QWidget w;
w.resize(300, 300);
w.show();
XReparentWindow(display, x11w, w.winId(), 0, 0);
XMapWindow(display, x11w); // must be performed after XReparentWindow,
// otherwise the window is not visible.
return a.exec();
}
To solve the problem through a widget ParentWindow failed - xwindow is embedded in QWidget, but have problems with resizing the window and closing it (it doesn't close).
QX11EmbedContainer could be what you need.
Let Qt create your Window and then use the Qt X11 Drawable with your X11/GL code.
With OpenGL and Qt you must use the Qt OpenGL context, if Qt is rendering using OpenGL. Just be aware that Qt expects the OpenGL state to be put back to what it was when it last used it.
You can get access to the Drawable using QX11Info (also check Compiler does not see QX11Info as this covers a common problem when including X11 with Qt).
The way Qt provides access to both X11 and OpenGL seems to change between major and minor versions so you may need to do a little bit of digging.
I know that the above works with Qt5.1 upto 5.5. Qt5.6 has problems with this approach that I've not yet resolved.
You should not touch window IDs in your first Qt program. Window IDs are a low level concept and a Qt programmer normally needs them only to do something outside of the Qt framework. Managing widgets as children of other widgets is not that kind of task.
I recommend you start with one of the tutorials. Look in particular here to see how you make a widget a child of another widget.
I want to change the background color of a QPlainTextEdit, how do I do this?
Modify the palette of your plain text edit. Sample program:
#include <QApplication>
#include <QPlainTextEdit>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QPlainTextEdit edit;
QPalette p = edit.palette();
p.setColor(QPalette::Active, QPalette::Base, Qt::red);
p.setColor(QPalette::Inactive, QPalette::Base, Qt::red);
edit.setPalette(p);
edit.show();
return app.exec();
}
Substitute whatever color you want, of course.
If QPlainTextEdit supports style sheets, you could do it like this:
myPlainTextEdit->setStyleSheet("background-color: yellow");
or
qApp->setStyleSheet("QPlainTextEdit {background-color: yellow}");
Slightly confusingly they call it role rather than colour/color.
https://doc.qt.io/qt-5/qwidget.html#setBackgroundRole
hint - if you can't find a function for a particular control, click on show inherited members - most general settings are in qWidget which is the basis for eveything drawn on screen.
May be you need to call QPlainTextEdit::setBackgroundVisible(true).
In order to modify the background, you need to modify the palette of your QPlainTextEdit and to set background visible:
myPlainTextEdit->setPalette(QPalette(/*Select the constructor you need*/));
myPlainTextEdit->setBackgroundVisible(true);