How do I create a simple widget that would contain a first, middle and last name field and add it to the main window class?
I ask because I cant figure out why is this simple widget attempt below is not working, what have I missed?
main
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainWindow class
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){
QMainWindow *mainView = new QMainWindow;
setCentralWidget(mainView);
CardUI *card = new CardUI;
QHBoxLayout *hCard = new QHBoxLayout;
hCard->addWidget(card);
mainView->setLayout(hCard);
mainView->show();
}
cardui class
CardUI::CardUI(QWidget *parent) : QWidget(parent){
QLineEdit *fnText = new QLineEdit;
QLineEdit *miText = new QLineEdit;
QLineEdit *lnText = new QLineEdit;
QHBoxLayout *name = new QHBoxLayout;
name->addWidget(fnText);
name->addWidget(miText);
name->addWidget(lnText);
setLayout(name);
}
QMainWindow *mainView = new QMainWindow;
//....
mainView->setLayout(hCard);
You should not change layout of QMainWindow. Use setCentralWidget or add toolbars/docks using given API instead.
In this particular case you shouldn't create mainView as QMainWindow (you cannot have two main windows in one application, right?). You can change mainView type to QWidget, but you can even don't create any proxy widgets, and just
MainWindow::MainWindow(QWidget *parent); : QMainWindow(parent){
card = new CardUI;
setCentralWidget(card);
}
Related
I have a QQuickwidget in a Qt C++ application where i have loaded a QML file (main.qml) and using QAction(actionstart) and C++ functions i have to load another QML file(main1.qml) slightly different to previous one on the same QQuickWidget object.
I am able to do this , but my 2nd QML file is overlapped from the middle section of QQuickwidget and further.
I have did this to stop overlapping of 2 QML files but not successful completely. count3 = 1 is defined in public section of Guiapplication.h file.
void GuiApplication::on_actionstart_triggered()
{
if (count3 == 1)
{
set_animation();
count3 = 2;
}
}
C++ Function for loading 1st QML file(main.qml)
void GuiApplication::rolling_animation()
{
QQuickView *quickWidget=new QQuickView();
QWidget *contain = QWidget::createWindowContainer(quickWidget,this);
contain->setMinimumSize(1008,349);
contain->setMaximumSize(1008,349);
contain->setFocusPolicy(Qt::TabFocus);
quickWidget->setSource(QUrl("qrc:/Resources/main.qml"));
ui->horizontalLayout_6->addWidget(contain);
}
C++ Function for loading 2nd QML file(main1.qml)
void GuiApplication::set_animation()
{
QQuickView *quickWidget=new QQuickView();
QWidget *Contain = QWidget::createWindowContainer(quickWidget,this);
Contain->setMinimumSize(1008,349);
Contain->setMaximumSize(1008,349);
Contain->setFocusPolicy(Qt::TabFocus);
quickWidget->setSource(QUrl("qrc:/Resources/main1.qml"));
ui->horizontalLayout_6->addWidget(Contain);
//ui->horizontalLayout_9->invalidate();
//ui->horizontalLayout_9->removeWidget(quickWidget_4);
}
output window image
Depending on whether you want to save or not the state of the QML that you want to hide there are the following alternatives:
Reject the same QQuickWidget (I recommend changing QQuickView to QQuickWidget) and just change the source.
#include <QtQuickWidgets>
class Widget: public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent=nullptr):
QWidget(parent),
m_widget(new QQuickWidget)
{
m_widget->setResizeMode(QQuickWidget::SizeRootObjectToView);
QPushButton *button1 = new QPushButton("show 1");
QPushButton *button2 = new QPushButton("show 2");
QHBoxLayout *lay = new QHBoxLayout(this);
QVBoxLayout *vlay = new QVBoxLayout;
vlay->addWidget(button1);
vlay->addWidget(button2);
lay->addLayout(vlay);
lay->addWidget(m_widget);
connect(button1, &QPushButton::clicked, this, &Widget::show1);
connect(button2, &QPushButton::clicked, this, &Widget::show2);
show1();
}
private slots:
void show1(){
m_widget->setSource(QUrl("qrc:/main1.qml"));
}
void show2(){
m_widget->setSource(QUrl("qrc:/main2.qml"));
}
private:
QQuickWidget *m_widget;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
#include "main.moc"
Use 2 QuickWidgets and use a QStackedWidget to toggle the widgets, with this method only hidden so the state of the QML will persist, in the previous case when changing the source is lost.
#include <QtQuickWidgets>
class Widget: public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent=nullptr):
QWidget(parent),
m_stacked_widget(new QStackedWidget)
{
for(const QString & url: {"qrc:/main1.qml", "qrc:/main2.qml"}){
QQuickWidget *widget = new QQuickWidget;
widget->setResizeMode(QQuickWidget::SizeRootObjectToView);
widget->setSource(QUrl(url));
m_stacked_widget->addWidget(widget);
}
QPushButton *button1 = new QPushButton("show 1");
QPushButton *button2 = new QPushButton("show 2");
QHBoxLayout *lay = new QHBoxLayout(this);
QVBoxLayout *vlay = new QVBoxLayout;
vlay->addWidget(button1);
vlay->addWidget(button2);
lay->addLayout(vlay);
lay->addWidget(m_stacked_widget);
connect(button1, &QPushButton::clicked, this, &Widget::show1);
connect(button2, &QPushButton::clicked, this, &Widget::show2);
show1();
}
private slots:
void show1(){
m_stacked_widget->setCurrentIndex(0);
}
void show2(){
m_stacked_widget->setCurrentIndex(1);
}
private:
QStackedWidget *m_stacked_widget;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
#include "main.moc"
The examples can be found here
I am currently learning to use Qt. So I am trying to create an application that uses QtWidgets. However, when I ran my program and resize manually the window obtained, the texts displayed are trimmed (see image)
.
The code I am using is shown below:
#include <QApplication>
#include "FenPrincipale.h"
using namespace std;
int main(int argc, char *argv[])
{
QApplication app(argc,argv);
FenPrincipale fenetre;
fenetre.show();
return app.exec();
}
and the FenPrincipale contains:
FenPrincipale::FenPrincipale()
{
QWidget mainFen;
QGroupBox *groupbox1 = new QGroupBox("Class definition", &mainFen);
QLineEdit *className = new QLineEdit;
QLineEdit *mother_className = new QLineEdit;
QFormLayout *class_def = new QFormLayout;
class_def->addRow("Class name", className);
class_def->addRow("Mother Class", mother_className);
groupbox1->setLayout(class_def);
QGroupBox *groupbox2 = new QGroupBox("Options", &mainFen);
QCheckBox *header_protect = new QCheckBox("Protect header against mutiple inclusions");
QCheckBox *constr_gen= new QCheckBox("Generate default constructor");
QCheckBox *destruct_gen= new QCheckBox("Generate a destructor");
QVBoxLayout *options_layout = new QVBoxLayout;
options_layout->addWidget(header_protect);
options_layout->addWidget(constr_gen);
options_layout->addWidget(destruct_gen);
groupbox2->setLayout(options_layout);
QVBoxLayout *main_layout = new QVBoxLayout;
main_layout->addWidget(groupbox1);
main_layout->addWidget(groupbox2);
this->setLayout(main_layout);
this->setWindowTitle("Zero Class Generator");
this->resize(400,450);
}
I don't have any idea how to fix this problem. Can anyone help me?
QWidget::setMinimumSize
should make the job
https://doc.qt.io/qt-5/qwidget.html#setMinimumSize-1
I am trying to show a QMainWindow inside a QDialog but the former does not appear.
I have subclassed QDialog, let's call it myDialog
A small example:
myDialog(QWidget *p_parent) : QDialog(p_parent)
{
QGridLayout *p_dialogLayout = new QGridLayout(this);
QMainWindow *p_MainWindow = new QMainWindow(this);
QLabel *p_label = new QLabel(this);
p_MainWindow->setCentralWidget(p_label);
QPushButton *p_cancel = new QPushButton("Cancel", this);
p_dialogLayout ->addWidget(p_MainWindow, 0, 0);
p_dialogLayout ->addWidget(p_cancel, 1, 0);
}
If I execute the dialog, I only see the button, not the embeded QMainWindow.
If i force to show the qmainwindow, the mainwindow is shown in another window.
Use QLayout::setMenuBar to add a toolbar to your dialog.
#include <QtWidgets>
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = nullptr) : QDialog(parent)
{
resize(600, 400);
setLayout(new QHBoxLayout);
QToolBar *toolbar = new QToolBar;
toolbar->addAction("Action one");
toolbar->addAction("Action two");
layout()->setMenuBar(toolbar);
layout()->addWidget(new QLabel("Label one"));
layout()->addWidget(new QLabel("Label two"));
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
#include "main.moc"
I don't think this is supported by the Qt framework, according to their documentation from here, it's intended to be used only once in an application.
My suggestion would be to take all your MainWindow implementation in a separate form (inheriting QWidget), and just add that form to your MainWindow in the constructor using something like p_MainWindow->setCentralWidget(p_YourNewForm);
I have been able to do it.
The trick is to construct the QMainWindow without a parent, and then apply the .setParent
Here is how:
myDialog(QWidget *p_parent) : QDialog(p_parent)
{
QGridLayout *p_dialogLayout = new QGridLayout(this);
QMainWindow *p_MainWindow = new QMainWindow(); //Without a parent
QLabel *p_label = new QLabel(this);
p_MainWindow->setCentralWidget(p_label);
QPushButton *p_cancel = new QPushButton("Cancel", this);
p_dialogLayout ->addWidget(p_MainWindow, 0, 0);
p_dialogLayout ->addWidget(p_cancel, 1, 0);
p_MainWindow->setParent(this); //Set the parent, to delete the MainWindow when the dialog is deleted.
}
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();
}
Greetings all,
Is there any widget to separate two QWidgets and also give full focus to a one widget.
As shown in following figure ?
Thanks in advance,
umanga
How about QSplitter?
QWidget 1, for exmaple, QListView. QWidget 2 is a combination of QWidgets (the left part is simple QPushButton with show/hide caption, and the right part another widget)... All you have to do, is to hide your QWidget2 when user clicked on QPushButton...
If you need an example, I may post it.
Updated
main.cpp
#include "splitter.h"
#include <QtGui/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
splitter w;
w.show();
return a.exec();
}
splitter.h
#ifndef SPLITTER_H
#define SPLITTER_H
#include <QtGui/QDialog>
class splitter : public QDialog
{
Q_OBJECT;
QWidget* widget1;
QWidget* widget2;
QPushButton* button;
public:
splitter(QWidget *parent = 0, Qt::WFlags flags = 0);
~splitter();
private slots:
void showHide(void);
};
#endif // SPLITTER_H
splitter.cpp
#include <QtGui>
#include "splitter.h"
splitter::splitter(QWidget *parent, Qt::WFlags flags)
: QDialog(parent, flags)
{
QApplication::setStyle("plastique");
QListView* listView = new QListView;
QTableView* tableView = new QTableView;
button = new QPushButton("Hide >");
widget1 = new QWidget;
QHBoxLayout* w1Layout = new QHBoxLayout;
w1Layout->addWidget(listView);
w1Layout->addWidget(button);
widget1->setLayout(w1Layout);
widget2 = new QWidget;
QHBoxLayout* w2Layout = new QHBoxLayout;
w2Layout->addWidget(tableView);
widget2->setLayout(w2Layout);
QSplitter *mainSplitter = new QSplitter(this);
mainSplitter->addWidget(widget1);
mainSplitter->addWidget(widget2);
connect(button, SIGNAL(clicked()), this, SLOT(showHide()));
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(mainSplitter);
setLayout(mainLayout);
}
splitter::~splitter()
{}
void splitter::showHide(void)
{
if (widget2->isVisible())
{ // hide
widget2->setVisible(false);
button->setText("< Show");
}
else
{ // show
widget2->setVisible(true);
button->setText("Hide >");
}
}