doLayout: how to setGeometry on top of another widget? - c++

I want to create custom Layout alike here: http://doc.qt.io/qt-5/qtwidgets-layouts-flowlayout-flowlayout-cpp.html
I want some methood to put checkbox on top of custom button. For now there are
setGeometry(QRect(QPoint(...
methods for either button and checkbox but whether I'm doing it for button or checkobox first still checkbox appears "under" the button and I can't see/click it.
How can I put checkbox on top of button there?

Simply make the checkbox a child of the button and call setGeometry relative to the button. Children are always drawn on top of their parents.
QPushButton button("Hello World!", &w);
button.setGeometry(0,0,100,100);
button.show();
QCheckBox checkBox(&button);
checkBox.setGeometry(button.rect());
checkBox.show();
No need to put the checkbox into a layout.

I have just made this snippet to check the checkbox on the top of the button and it works for me.
#include "mainwindow.h"
#include <QApplication>
#include <QPushButton>
#include <QCheckBox>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
QPushButton button("Hello World!", &w);
button.setGeometry(0,0,100,100);
button.show();
QCheckBox checkBox(&w);
checkBox.setGeometry(30,30,50,50);
checkBox.show();
w.show();
return a.exec();
}
and here is if you will change the order of "parenting" and want checkbox still be on the top:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
QCheckBox checkBox(&w);
checkBox.setGeometry(30,30,50,50);
checkBox.show();
QPushButton button("Hello World!", &w);
button.setGeometry(0,0,100,100);
button.show();
checkBox.setParent(NULL);
checkBox.setParent(&w);
w.show();
return a.exec();
}

Related

How to hide QMainWindow and show splashcreen during startup?

I am trying to hide the MainWindow of my Qt desktop app during startup, and to show a splashscreen. Both only happens after the loading phase, even though I call both splash.show() and window.hide() before the loading phase. I tried to split loading phase and constructor, but result is the same. How can I achieve both before the loading phase ?
Update 1
To display the splash screen, I had to add a call to QApplication::processEvents()
Update 2
The black window was actually not the MainWindow, but a ghost window that popped because scrollArea->setVisible(true) was called in the constructor.
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPixmap pixmap(QStringLiteral(":/ressources/icons/icon.png"));
QSplashScreen splash(pixmap);
splash.show();
MainWindow window; // this loads for 5-6 seconds
a.processEvents();
window.showLoginPrompt();
splash.finish(&window);
return a.exec();
}
Based on your code and some example I could make it run like you are trying to do.
You only need to call your promptLogin function instead.
#include <QApplication>
#include <QTimer>
#include <QSplashScreen>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QSplashScreen *splash = new QSplashScreen;
splash->setPixmap(QPixmap("D:\\Projects\\SplashScreen\\TestSplashScreen\\splash.png"));
splash->show();
MainWindow mainWin;
QTimer::singleShot(2500, splash, SLOT(close()));
QTimer::singleShot(2500, &mainWin, SLOT(show()));
return app.exec();
}

Show the status of icons in toolbar using qt in C++

I would like to show the status of icons in toolbar (whether they are activated). For example, When I click Bold, Italic or Underline icon in Microsoft word, it will be shaded, and switch to normal status when I click it again.
It's not necessary to be shaded. I just need to distinguish whether it is activated.
You have to checkable the QAction, or use a QWidget that is checkable like QToolButton:
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow w;
QToolBar *toolbar = w.addToolBar("Toolbar");
QAction *bold_action = toolbar->addAction("B");
QFont bold_fn(bold_action->font());
bold_fn.setBold(true);
bold_action->setFont(bold_fn);
bold_action->setCheckable(true);
QAction *italic_action = toolbar->addAction("I");
QFont fn_cursive(italic_action->font());
fn_cursive.setItalic(true);
italic_action->setFont(fn_cursive);
italic_action->setCheckable(true);
QAction *underline_action = toolbar->addAction("U");
QFont fn_underline(underline_action->font());
fn_underline.setUnderline(true);
underline_action->setFont(fn_underline);
underline_action->setCheckable(true);
QAction* subscript_action = new QAction;
subscript_action->setIcon(QIcon(":/subscript.png"));
subscript_action->setCheckable(true); // <---
toolbar->addAction(subscript_action);
w.setCentralWidget(new QTextEdit);
w.resize(320, 240);
w.show();
return a.exec();
}
Output:

Remove space between QWidgets

With the following code I generate a window with buttons:
#include <QApplication>
#include <QtWidgets>
class Item : public QWidget {
QHBoxLayout hLayout{this};
QPushButton bt{"button"};
public:
Item() : QWidget() {
hLayout.addWidget(&bt);
}
};
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QWidget w;
QVBoxLayout vLayout(&w);
vLayout.addWidget(new Item());
vLayout.addWidget(new Item());
vLayout.addWidget(new Item());
vLayout.addWidget(new Item());
vLayout.addWidget(new Item());
w.show();
return app.exec();
}
With layout.setContentsMargins(0,0,0,0) I can go to:
Is it possible to reduce even more space between buttons?
You can try using setSpacing method :
vLayout.setSpacing(0);
But I think the real issue here would be that your layout is stretched out to cover whole widget and it arranges layout items accordingly.
Another thing you can try is to set the margin :
vLayout.setMargin(0);
You can remove the space, by adjusting the spacing:
http://doc.qt.io/qt-5/qlayout.html#spacing-prop
Setting it to 0 should bring the widgets together.
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QWidget w;
QVBoxLayout vLayout(&w);
vLayout.addWidget(new Item());
...
// Removes outer margins
vLayout.setContentsMargins(0,0,0,0);
// Set space between items to '0'
vLayout.setSpacing(0);
w.show();
return app.exec();
}
In your example above you should probably also add a strech section:
http://doc.qt.io/qt-5/qboxlayout.html#addStretch

Requires 2 right clicks to switch context menus among items?

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.

Cannot press QPushButton in a simple program

Basically, I want a simple pushButton with a colorful text which when pressed exits the application.
Why cant I press PushButton in this simple program. I am using QT 4.6 on Arch x86_64.
#include <QtGui/QApplication>
#include <QLabel>
#include <QPushButton>
#include<QtGui>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow *Main=new QMainWindow;
QPushButton *button = new QPushButton(Main);
QLabel *label = new QLabel(Main);
label->setText("<h2><i>Hello</i> ""<font color=red>Qt!</font></h2>");
label->setVisible(true);
QObject::connect(button, SIGNAL(clicked()),label, SLOT(close()));
label->setAlignment(Qt::AlignCenter|Qt::AlignVCenter);
label->setWindowTitle("HelloWorld Test Program");
Main->show();
return a.exec();
}
Beside the use of a button class that will allow you to display rich text, you also need to make sure your connections are correct.
In your example, you're connecting the clicked signal of the button to the clear() slot of the label, which is non-sense.
To exit your app when the button is clicked, you need to close the main window. Here is the code to get the right connection :
QObject::connect(button, SIGNAL(clicked()),Main, SLOT(close()));
Changing this single line of code in your example is not enough, because your label is drawn on top of your button, so it's not possible to graphically click on it. You need to hide your label and put some text into your button :
button->setText("Hello");
label->setVisible(false);
Regarding the rich text feature in a QPushButton, AFAIK it is not possible to do it with a QPushButton.
UPDATE :
Here is a way to put some richtext on a QPushButton. It uses the solution described by my comment : painting a QTextDocument onto a pixmap and setting this pixmap as the button's icon.
#include <QtGui/QApplication>
#include <QLabel>
#include <QPushButton>
#include <QtGui>
#include <QTextDocument>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow *Main=new QMainWindow;
QPushButton *button = new QPushButton(Main);
QTextDocument Text;
Text.setHtml("<h2><i>Hello</i> ""<font color=red>Qt!</font></h2>");
QPixmap pixmap(Text.size().width(), Text.size().height());
pixmap.fill( Qt::transparent );
QPainter painter( &pixmap );
Text.drawContents(&painter, pixmap.rect());
QIcon ButtonIcon(pixmap);
button->setIcon(ButtonIcon);
button->setIconSize(pixmap.rect().size());
QObject::connect(button, SIGNAL(clicked()),Main, SLOT(close()));
Main->show();
return a.exec();
}
Take a look here. Widget called QwwRichTextButton.
The QwwRichTextButton widget provides a button that can display rich text.