Cannot press QPushButton in a simple program - c++

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.

Related

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:

Fill background outside window QT

I have read a documentation and aswell many sites on website.
Unfortunatly I didn't find answer to my question.
Is there any chance(I believe there is) to fill background outside the popup window? Let me explain: If i have window like of my whole app with resolution 500x500 [px] and I create a popup window 300x300 in the middle - it means I have 200 px in each side "parent-window". Is there any chance (method, flag) to fill background in gray color?
Image: https://imgur.com/Hunev58
Modifying the palette does the job. Here you get a purple background when the MessageBox is shown , and come back to normal once clicked
#include <QApplication>
#include <QMainWindow>
#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QMessageBox>
#include <QPalette>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow w;
QVBoxLayout mainLayout;
QLabel foolabel("FooLabel");
QPushButton foobutton("FooButton");
mainLayout.addWidget(&foolabel);
mainLayout.addWidget(&foobutton);
QWidget window;
window.setLayout(&mainLayout);
w.setCentralWidget(&window);
QPalette palette = QApplication::palette(&w);
palette.setColor(QPalette::Inactive,QPalette::Window,QColor(138,43,226));
w.setPalette(palette);
w.show();
QMessageBox box(&w);
box.exec();
return a.exec();
}

doLayout: how to setGeometry on top of another widget?

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();
}

How to create right GUI widget by using Qt?

I'm writing a simple text editor by using Qt and Qt Creator. I wonder how to make right application's structure. I mean widgets. Is QMainWindow should be main widget or it can be QWidget? When I trying to specify QMainWindiw as QTextEdit's parent widget, QTextEdit is not displayed. Because of it I decided to initialize QMainWindow as QWidget's parent and QWidget became a parent widget for all another widgets. Is it a right way?
#include <QApplication>
#include <QMainWindow>
#include <QWidget>
#include <QVBoxLayout>
#include <QTextEdit>
#include <QMenuBar>
#include <QMenu>
#include <QSizePolicy>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow *mainWindow = new QMainWindow;
QMenu *fileMenu = new QMenu("File");
fileMenu->addAction("New");
fileMenu->addAction("Open");
fileMenu->addAction("Save");
fileMenu->addAction("Save as");
fileMenu->addSeparator();
fileMenu->setMaximumWidth(160);
QMenu *editMenu = new QMenu("Edit");
editMenu->addAction("Copy");
editMenu->addAction("Past");
editMenu->addAction("Cut");
editMenu->setMinimumWidth(160);
QMenuBar *mainMenu = new QMenuBar;
mainMenu->addMenu(fileMenu);
mainMenu->addMenu(editMenu);
mainMenu->addAction("Exit");
mainMenu->show();
QWidget *mainWidget = new QWidget(mainWindow);
mainWidget->move(0, 20);
mainWidget->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
QTextEdit *textEdit = new QTextEdit;
textEdit->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
QVBoxLayout *vBoxLayout = new QVBoxLayout;
vBoxLayout->addWidget(textEdit);
mainWidget->setLayout(vBoxLayout);
mainWidget->show();
mainWindow->setMenuBar(mainMenu);
mainWindow->show();
return a.exec();
}
You should use QMainWindow if you need to use one of its features: toolbars, dock widgets, main menu or status bar (see QMainWindow docs for more information). If you don't need them, you can use QWidget as your top level widget.
When working with QMainWindow, you need to set central widget using QMainWindow::setCentralWidget and add window contents to this widget, not to the QMainWindow itself.

Adding a label to a widget

I am trying to add a label to the main window using Qt. Here is a piece of the code:
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QWidget Main_Window;
QPixmap Image;
Image.load("1837.jpg");
QLabel i_label;
i_label.setPixmap(Image);
i_label.show();
QPushButton Bu_Quit("Quit", &Main_Window);
QObject::connect(&Bu_Quit, SIGNAL(clicked()), qApp, SLOT(quit()));
Main_Window.show();
return app.exec();
}
I've been having a very hard time figuring out how to properly add QLabels to QWidgets, I tried to set the Main_Window as the main widget using this method: app.setMainWidget(Main_Window) and the label was still outside the window. So how do I put labels into widgets using Qt?
hamza, this code worked fine for me:
#include <QtGui>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget Main_Window;
QLabel i_label("Start", &Main_Window);
//i_label.setPixmap(QPixmap("1837.jpg"));
QPushButton Bu_Quit("Quit" , &Main_Window);
QObject::connect(&Bu_Quit , SIGNAL(clicked()), qApp , SLOT(quit()));
QVBoxLayout *vbl = new QVBoxLayout(&Main_Window);
vbl->addWidget(&i_label);
vbl->addWidget(&Bu_Quit);
Main_Window.show();
return app.exec();
}
I commented setting the image code to show you that the label was set correctly. Make sure your image is valid (otherwise you won't see the text). The trick here was that you need to use qt layouts like QVBoxLayout
Add the label to a layout widget and set the window layout to that layout.
Design note: its better to create your own MainWindow class, inheriting from QMainWindow for instance, and design it from the inside.
or even better, use QtCreator.
You can try:
QWidget window;
QImage image("yourImage.png");
QImage newImage = image.scaled(150, 150, Qt::KeepAspectRatio);
QLabel label("label", &window);
label.setGeometry(100, 100, 100, 100);
label.setPixmap(QPixmap::fromImage(newImage));
window.show();
this way you can even decide where to put the label and choose the image size.