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.
Related
What is the procedure to make a text being written in a QLineEdit widget, dynamically display inside a QTextEdit that already contains some text?
For example, let us say that a QLineEdit asks for a name where one writes "John". Is it possible to display it in real time inside a QTextEdit containing :
The name is + textFromQLineEdit + , age 24 ?
The displayed text has to dynamically take into account the changes being made to the QLineEdit so that the user does not need to press a button or press enter to see his/her name appear.
The following is the minimal code for connecting the two widgets to each other using the signal textChanged() from QLineEdit and the slot setText() from QTextEdit (which does not allow for adding some text before and after the text from the QLineEdit) :
#include <QLineEdit>
#include <QVBoxLayout>
#include <QGroupBox>
#include <QTextEdit>
#include <QApplication>
class SmallWindow : public QWidget
{
Q_OBJECT
public:
SmallWindow();
private:
QLineEdit *nameLine;
QTextEdit *textBox;
};
SmallWindow::SmallWindow() : QWidget()
{
setFixedSize(300,250);
QLineEdit *nameLine = new QLineEdit;
QTextEdit *textBox = new QTextEdit;
QWidget::connect(nameLine,SIGNAL(textChanged(QString)),textBox,SLOT(setText(QString)));
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(nameLine);
layout->addWidget(textBox);
QGroupBox *group = new QGroupBox(this);
group->setLayout(layout);
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
SmallWindow window;
window.show();
app.exec();
}
#include "main.moc"
What should be done to keep the text before and after the QLineEdit text in place and updating the QTextEdit box in real time?
Create special slot:
void SmallWindow::pasteText(const QString& str)
{
textBox->setText(QString("The name is %1 , age 24").arg(str));
}
and don't use textChanged() signal because you need only one name accepted by user, so you need QLineEdit::editingFinished() (or maybe QLineEdit::returnPressed(), it depends on your needs)
connect(nameLine,SIGNAL(editingFinished(QString)),this,SLOT(pasteText(QString)));
Also you don't need QWidget::connect, because you write this code inside QObject subclass, so it is not necessary.
Also these lines:
QLineEdit *nameLine = new QLineEdit;
QTextEdit *textBox = new QTextEdit;
should be:
nameLine = new QLineEdit;
textBox = new QTextEdit;
Create an own slot for the text update. I think that you have also some errors in your code.
Widgets nameLine and textBox are already defined in the SmallWindow.h. You probably want to create them in SmallWindow.cpp following way:
nameLine = new QLineEdit;
textBox = new QTextEdit;
Also GroupBox group is not set to any layouts. Perhaps you want to create one layout more and set the widget there?
QVBoxLayout *mainlayout = new QVBoxLayout;
mainlayout->addWidget(group);
this->setLayout(mainlayout);
If you create an own slot for the text update, you can just change text content of the textBox there:
SmallWindow.h
#ifndef SMALLWINDOW_H
#define SMALLWINDOW_H
#include <QLineEdit>
#include <QVBoxLayout>
#include <QGroupBox>
#include <QTextEdit>
class SmallWindow : public QWidget
{
Q_OBJECT
public:
SmallWindow();
private slots:
void updateLineEditText(QString name);
private:
QLineEdit *nameLine;
QTextEdit *textBox;
};
#endif // SMALLWINDOW_H
SmallWindow.cpp
#include "SmallWindow.h"
SmallWindow::SmallWindow() : QWidget()
{
setFixedSize(300,250);
nameLine = new QLineEdit;
textBox = new QTextEdit;
connect(nameLine,SIGNAL(textChanged(QString)),this,
SLOT(updateLineEditText(QString)));
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(nameLine);
layout->addWidget(textBox);
QGroupBox *group = new QGroupBox(this);
group->setLayout(layout);
QVBoxLayout *mainlayout = new QVBoxLayout;
mainlayout->addWidget(group);
this->setLayout(mainlayout);
}
void SmallWindow::updateLineEditText(QString name) {
QString textEditString("The name is ");
textEditString.append(name);
textEditString.append(", age 24 ?");
textBox->setText(textEditString);
}
This is a minimal example in Qt 5, using C++11. It is about as concise as it would be in Python. If you are using Qt 5, then your question should have looked exactly as it does below, save for the connect statement. This is what "minimal" means when it comes to Qt. Avoid the fluff and boilerplate that doesn't add to the problem. There's no need to have a separate class for the window in such a simple example.
#include <QVBoxLayout>
#include <QLineEdit>
#include <QTextEdit>
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget window;
QVBoxLayout layout(&window);
QLineEdit name;
QTextEdit text;
layout.addWidget(&name);
layout.addWidget(&text);
QObject::connect(&name, &QLineEdit::textChanged, [&](const QString & name){
text.setPlainText(QString("The name is %1, age 24.").arg(name));
});
window.show();
return app.exec();
}
The same in Qt 4 is below - note the absence of any manual memory management. That's how your question ideally should have looked for Qt 4, without the slot's implementation of course. You can use the connectSlotsByName or an explicit connect, of course - that's just a matter of style.
#include <QVBoxLayout>
#include <QLineEdit>
#include <QTextEdit>
#include <QApplication>
class Window : public QWidget {
Q_OBJECT
QVBoxLayout m_layout; // not a pointer!
QLineEdit m_name; // not a pointer, must come after the layout!
QTextEdit m_text;
Q_SLOT void on_name_textChanged(const QString & name) {
m_text.setPlainText(QString("The name is %1, age 24.").arg(name));
}
public:
Window() : m_layout(this) {
m_layout.addWidget(&m_name);
m_layout.addWidget(&m_text);
m_name.setObjectName("name");
QMetaObject::connectSlotsByName(this);
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Window window;
window.show();
return app.exec();
}
#include "main.moc"
I like to start out saying that I'm about as noob as they come. This is my first c++ program I built from scratch. I gotten most of the bugs ironed out, however I can't seem to be able to assign a value to a QLabel. I want the function 'value' to be called when the calculate button is pressed. The function 'value' should then do math and return the answer which is then assigned to the QLabel 'results'. Here is what I have so far.
#include <QApplication>
#include <QPushButton>
#include <QLabel>
#include <QSlider>
#include <QString>
#include <QSpinBox>
#include <QHBoxLayout>
#include <QComboBox>
double x;
double value(QSpinBox *spinner)
{
int speed;
speed = spinner->value();
x = speed/8;
return x;
}
int main(int argc, char *argv[])
{
QApplication prog(argc, argv);
QWidget *mainWindow = new QWidget;
mainWindow->setWindowTitle("Plex Calculator");
QPushButton *calculate = new QPushButton("Calculate");
QComboBox *kbormb = new QComboBox;
QSpinBox *spinner =new QSpinBox;
QSlider *slider = new QSlider(Qt::Horizontal);
QLabel *results = new QLabel;
spinner->setRange(1,1000);
slider->setRange(1,1000);
spinner->setValue(1);
QObject::connect(spinner, SIGNAL(valueChanged(int)),slider, SLOT(setValue(int)));
QObject::connect(slider, SIGNAL(valueChanged(int)),spinner, SLOT(setValue(int)));
kbormb->addItem("kb/s");
kbormb->addItem("mb/s");
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(slider);
layout->addWidget(spinner);
layout->addWidget(kbormb);
layout->addWidget(calculate);
layout->addWidget(results);
QObject::connect(calculate, SIGNAL(clicked()), &prog, SLOT(results->setNum(value(*spinner));));
mainWindow->setLayout(layout);
mainWindow->show();
return prog.exec();
}
The issue is with following signal slot connection.
QObject::connect(calculate, SIGNAL(clicked()), &prog, SLOT(results->setNum(value(*spinner));));
You are trying to connect the clicked() signal of calculate button to results->setNum(value(*spinner)); slot of prog. But results->setNum(value(*spinner)); is not actually a slot.
A slot is simply a method in a class that inherits QObject. Method should be added under slots: section in the class. Read more about signals and slots here.
To fix this, you will have to create a separate class for your widget and add the logic their. you can add a slot to the newly created class and connect the clicked() signal to that. Then you can do the calculation in the slot.
I have a custom widget with some standard child widgets inside. If I make a separate test project and redefine my custom widget to inherit QMainWindow, everything is fine. However, if my custom widget inherits QWidget, the window opens, but there are no child widgets inside.
This is the code:
controls.h:
#include <QtGui>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QPushButton>
class Controls : public QWidget
{
Q_OBJECT
public:
Controls();
private slots:
void render();
private:
QWidget *frame;
QWidget *renderFrame;
QVBoxLayout *layout;
QLineEdit *rayleigh;
QLineEdit *mie;
QLineEdit *angle;
QPushButton *renderButton;
};
controls.cpp:
#include "controls.h"
Controls::Controls()
{
frame = new QWidget;
layout = new QVBoxLayout(frame);
rayleigh = new QLineEdit;
mie = new QLineEdit;
angle = new QLineEdit;
renderButton = new QPushButton(tr("Render"));
layout->addWidget(rayleigh);
layout->addWidget(mie);
layout->addWidget(angle);
layout->addWidget(renderButton);
frame->setLayout(layout);
setFixedSize(200, 400);
connect(renderButton, SIGNAL(clicked()), this, SLOT(render()));
}
main.cpp:
#include <QApplication>
#include "controls.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Controls *controls = new Controls();
controls->show();
return app.exec();
}
This opens up a window with correct dimensions, but with no content inside.
Bear in mind this is my first day using Qt. I need to make this work without inheriting QMainWindow because later on I need to put this on a QMainWindow.
You're missing a top level layout:
Controls::Controls()
{
... (yoour code)
QVBoxLayout* topLevel = new QVBoxLayout(this);
topLevel->addWidget( frame );
}
Or, if frame is not used anywhere else, directly:
Controls::Controls()
{
layout = new QVBoxLayout(this);
rayleigh = new QLineEdit;
mie = new QLineEdit;
angle = new QLineEdit;
renderButton = new QPushButton(tr("Render"));
layout->addWidget(rayleigh);
layout->addWidget(mie);
layout->addWidget(angle);
layout->addWidget(renderButton);
setFixedSize(200, 400);
connect(renderButton, SIGNAL(clicked()), this, SLOT(render()));
}
Note that setLayout is done automatically when QLayout is created (using parent widget)
You'll want to set a layout on your Controls class for managing its child sizes. I'd recommend removing your frame widget.
controls.cpp
Controls::Controls()
{
layout = new QVBoxLayout(this);
.
.
.
}
main.cpp
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
MainWindow w;
w.show();
return app.exec();
}
Hi I'm trying to make a simple layout in Qt and first of all the layout is not working properly at all, all that showed up was a cancel button. So I have been messing around and now when I run it it runs without errors but no window pops up, don't know what I could have done to cause this? Here is my code
#ifndef FILMINPUT_H
#define FILMINPUT_H
#include <QMainWindow>
#include "Film.h"
#include "FilmWriter.h"
#include <QLabel>
#include <QTextEdit>
#include <QPushButton>
namespace Ui {
class FilmInput;
}
class FilmInput : public QMainWindow
{
Q_OBJECT
public:
explicit FilmInput(QWidget *parent = 0);
~FilmInput();
private:
Ui::FilmInput *ui;
//widgets
QMainWindow* window;
QMenuBar* menubar;
QLabel* infoLabel;
QLabel* titleLabel;
QLabel* durationLabel;
QLabel* directorLabel;
QLabel* relDateLabel;
QTextEdit* titleEdit;
QTextEdit* durationEdit;
QTextEdit* directorEdit;
QTextEdit* relDateEdit;
QPushButton* saveBtn;
QPushButton* cancelBtn;
Film f;
//sets up gui and connects signals and slots
void setUpGui();
};
#endif // FILMINPUT_H
#include "filminput.h"
#include "ui_filminput.h"
#include <QtGui>
FilmInput::FilmInput(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::FilmInput)
{
ui->setupUi(this);
setUpGui();
}
FilmInput::~FilmInput()
{
delete ui;
}
void FilmInput::setUpGui(){
//initialise widgets
infoLabel = new QLabel("Please enter film data which will be saved to a file",this);
titleLabel = new QLabel("Film Title",this);
durationLabel = new QLabel("Film Duration",this);
directorLabel = new QLabel("Film Director",this);
relDateLabel = new QLabel("Film Release Date",this);
titleEdit = new QTextEdit(this);
durationEdit = new QTextEdit(this);
directorEdit = new QTextEdit(this);
relDateEdit = new QTextEdit(this);
saveBtn = new QPushButton("Save Film",this);
cancelBtn = new QPushButton("Cancel",this);
//set layout
QVBoxLayout* layout = new QVBoxLayout();
layout->setMenuBar(menubar);
layout->addWidget(infoLabel);
layout->addWidget(titleLabel);
layout->addWidget(durationLabel);
layout->addWidget(directorLabel);
layout->addWidget(relDateLabel);
layout->addWidget(titleEdit);
layout->addWidget(durationEdit);
layout->addWidget(directorEdit);
layout->addWidget(relDateEdit);
layout->addWidget(saveBtn);
layout->addWidget(cancelBtn);
this->setLayout(layout);
this->setWindowTitle("Film Archive");
}
#include <QtGui/QApplication>
#include "filminput.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
FilmInput w;
w.show();
return a.exec();
}
It looks like you have conflicting things here.
You have Qt's WYSIWYG widget editor (QtDesigner), which you tell Qt to initialize (ui->setupUi(this)):
#include "ui_filminput.h" //<---- Generated from the QtDesigner form
FilmInput::FilmInput(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::FilmInput) //<---- Creating the struct that holds of the widget pointers.
{
ui->setupUi(this); //<---- Telling Qt to setup and layout all the QtDesigner widgets from this designer form.
//setUpGui(); <--- Where your layouts and widgets are accidentally clashing with the form's widgets.
}
Then you also have the ones you manually are creating inside setUpGui(). It's fine to mix the QtDesigner forms with manually created widgets - I do it all the time. But what you accidentally are doing is you are accidentally setting a layout:
this->setLayout(layout);
On this main window.... which the QtDesigner form already did for its widgets, overwriting them, and possibly confusing the layout of the main window.
You can either remove the QtDesigner widgets entirely, or prefereably, make them interact nicely by setting your layout on a subwidget of your main window.
You can access the QtDesigner widgets through the 'ui' member-variable.
this->ui->someNameOfWidgetInQtDesigner
I believe the main window has a widget already created in QtDesigner called "centralWidget", or something similar (open up FilmInput.ui and check the actual naming).
So you should set your layout on that, assuming you didn't already create a layout in QtDesigner.
this->ui->centralWidget->setLayout(layout);
If your QtDesigner form (FilmInput.ui) already has a layout set on the centralWidget, add a new QWidget in the designer form as a child of the centralWidget in centralWidget's layout, and name it something like 'sidePanel' or whatever makes sense, and then do:
this->ui->sidePanel->setLayout(layout);
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.