I need to display a hierarchical set of data in a qt view. I'm using QColumnView to display the model. However, there is a feature such that the last column in the view will be relegated to a preview widget. Is it possible to hide this? Ex, something like view.setPreviewWidget( NULL ), although this breaks the program
EDIT : I should clarify that I'd like a way to hide the last column entirely, ie to have the last column in my view be the "leaves" of the model, and to not have a preview space
This will hide the button when it is clicked.
#include <QtGui/QApplication>
#include <QtGui/QColumnView>
#include <QtGui/QPushButton>
#include <QtGui/QFileSystemModel>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QColumnView view;
QFileSystemModel model;
QPushButton button(&view);
button.setText("Click me");
QObject::connect(&button, SIGNAL(clicked()), &button, SLOT(hide()));
model.setRootPath("/");
view.setModel(&model);
view.setPreviewWidget(&button);
view.show();
return a.exec();
}
Notice that it will become hidden forever. You have to call show() if you want it to be displayed again.
Related
I have a Qt program which was made without Qt Creator. And I want to improve the functionality: put all that i have now (QGridLayout, QLabels, QPushButtons and QCustomPlot) in one tab of QTabWidget and make another one tab with another layout and widgets. How should i do it correctly? I tried to create QTabWidget and add to it my QGridLayout with all stuff. Mb it is right way.
Tried to find some info but all are using creator for their projects. i dont like it and so i cant find examples of how can i push all ready functionality to qtabwidget
This is my main function:
int main(int argc, char **argv) {
QApplication a(argc, argv);
Smartcalc calc;
calc.resize(750, 500);
calc.show();
return a.exec();
}
This is my constructor:
Smartcalc::Smartcalc(QWidget *parent) : QWidget(parent) {
createWidgets();
initGraph(customPlot);
mainLayout = new QGridLayout;
addWidgetsToLayout(mainLayout);
setWindowTitle(tr("Smartcalc_v1.0"));
connectWidgets();
customWidgets();
}
So i need to create two tabwidgets and push my Smartcalc functionality in one of tabwidgets.
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();
}
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.
What I want to do with this piece of code is set the focus on the QMenu Item programmatically. But neither QMenu::setActiveAction() nor QMenu::popup() works.
How can I do that?
#include <QApplication>
#include <QMainWindow>
#include <QMenuBar>
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QMainWindow *window = new QMainWindow();
window->setWindowTitle(QString::fromUtf8("Test:QMenu"));
window->resize(336, 227);
QAction *newAct = new QAction("&New",window);
QAction *openAct = new QAction("&Open",window);
QAction *saveAct = new QAction("&Save",window);
QMenu *fileMenu;
fileMenu = window->menuBar()->addMenu("&File");
fileMenu->addAction(newAct);
fileMenu->addAction(openAct);
fileMenu->addAction(saveAct);
window->show();
fileMenu->popup(QPoint(10,10));
return app.exec();
}
QMenu items are not "focusable" in the same manner as other widgets. And, actually, they shouldn't, because what you want is not common practice of their usage.
As a workaround, on mouse press you can get mouse cursor position, pre-calculate offset of your default menu item in the popup menu and show the menu at the point, where mouse cursor underly your default menu item. This solution was suggested here.
Also, what will be more nice for user, to select default menu item you can generates narrow keys buttons events after you display your popup. This works on Windows, but not sure about other OS.
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.