Iam a absolute Beginner in QT..
i am trying to create window that has only text and one push button when you press it, you will get another window that has menu for program ..
but Unfortunately, i didn't know how can i create new window and connect it with main window!
so, i need to helping you
Here is a sample of main.cpp that do exactly that (you will have to modify the new window though).
#include <QtGui>
int main(int argc, char* argv[]) {
QApplication app(argc, argv);
QWidget *firstWindow = new QWidget();
QLabel *text = new QLabel("Here is some text one the first window.");
QPushButton *button = new QPushButton("Button on the first window that display the other window");
QBoxLayout *layout = new QVBoxLayout();
layout->addWidget(text);
layout->addWidget(button);
firstWindow->setLayout(layout);
QWidget *secondWindow = new QWidget();
// add some things on the second window
// on button click, close the first window and show the second one
connect(button, SIGNAL(clicked(bool)), secondWindow, SLOT(show()));
connect(button, SIGNAL(clicked(bool)), firstWindow, SLOT(close()));
// display the first window at the start of the application.
firstWindow->show();
return app.exec();
}
Related
I want to fit (child) widget into the parent widget size. So if the parent window is too small to display all the elements of the child widget the QScrollArea should appear otherwise it should be invisible.
I have attached the pictures for a better understanding.
The black box is where I want my scroll to appear. Since when we reduce the size of the window, sometimes you can't see the scroll bar (as displayed in the below picture) it doesn't look elegant enough for big projects.
Please help me with the same, thanks in advance.
Here's the sample code that I used for example:
int main(int argc, char *argv[]){
QApplication a(argc, argv);
QScrollPractice w;
QDialog * dlg = new QDialog();
//dlg->setGeometry(100, 100, 260, 260);
dlg->setMinimumSize(150, 200);
QScrollArea *scrollArea = new QScrollArea(dlg);
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scrollArea->setWidgetResizable(true);
//scrollArea->setGeometry(10, 10, 200, 200);
//scrollArea->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Ignored);
//QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
scrollArea->setSizePolicy(sizePolicy);
QWidget *widget = new QWidget(scrollArea);
scrollArea->setWidget(widget);
QVBoxLayout *layout = new QVBoxLayout(widget);
widget->setLayout(layout);
for (int i = 0; i < 10; i++)
{
QPushButton *button = new QPushButton(QString("%1").arg(i));
layout->addWidget(button);
}
dlg->show();
return a.exec();
}
Your Dialog is missing a layout as well. Thats the reason the scrollArea Widget isnt spread out across the dialog.
#include <QApplication>
#include <QDialog>
#include <QScrollArea>
#include <QVBoxLayout>
#include <QPushButton>
int main(int argc, char* argv[]){
QApplication a(argc, argv);
QDialog* dlg = new QDialog();
dlg->setMinimumSize(150, 200);
QScrollArea* scrollArea = new QScrollArea(dlg);
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scrollArea->setWidgetResizable(true);
QWidget* widget = new QWidget(scrollArea);
scrollArea->setWidget(widget);
QVBoxLayout* dlgLayout = new QVBoxLayout();
dlg->setLayout( dlgLayout );
dlgLayout->addWidget( scrollArea );
QVBoxLayout* layout = new QVBoxLayout(widget);
widget->setLayout(layout);
for (int i = 0; i < 10; i++)
{
QPushButton* button = new QPushButton(QString("%1").arg(i));
layout->addWidget(button);
}
dlg->show();
return a.exec();
}
I modified your code to make it run and compileable, also I added antoher QVBoxLayout and added it to the dialog. Then the scrollArea gets added to that Layout. Hope this helps.
I am writing a Qt Console Application using Qt Creator 4.6.0 in Linux. I would like to show a QDialog but I do not want it to
show an entry on taskbar, and
steal focus from other windows.
How can I do this?
I found kind of similar question, but the solutions does not work for me as it seems that I can't use this in a console application.
Here is what I have so far which shows the dialog but it neither hides it from taskbar, nor prevents it from stealing the focus:
QDialog splash;
QVBoxLayout *laySplash = new QVBoxLayout(&splash);
splash.setAttribute(Qt::WA_ShowWithoutActivating);
splash.setWindowFlags(Qt::Tool | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
QLabel *lblText = new QLabel;
laySplash->addWidget(lblText);
lblText->setText(QString::fromStdString("test"));
QTimer::singleShot(1000, &splash, SLOT(close()));
splash.exec();
The set parameters with exec() seems contradicting and won't prevent showing a blocking modal dialog that steals focus, if you just show() the dialog instead of exec(), other settings work. below code was tested on Debian Ubuntu 17.10, and achieves desired results
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//
QDialog splash;
QVBoxLayout *laySplash = new QVBoxLayout(&splash);
splash.setAttribute(Qt::WA_ShowWithoutActivating);
splash.setWindowFlags(Qt::Tool | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
QLabel *lblText = new QLabel;
laySplash->addWidget(lblText);
lblText->setText(QString::fromStdString("test"));
QTimer::singleShot(5000, &splash, SLOT(close()));
splash.show();
//
return a.exec();
}
Update:
If the code should work before or without a main event loop (before a.exec() is called or even without at all calling a.exec()), you need to enter an event loop to host your dialog, this can be repeated for each additional code separately; eventually you might opt to return any int value depending on your code.
#include <QEventLoop>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//
QDialog splash;
QVBoxLayout *laySplash = new QVBoxLayout(&splash);
splash.setAttribute(Qt::WA_ShowWithoutActivating);
splash.setWindowFlags(Qt::Tool | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
QLabel *lblText = new QLabel;
laySplash->addWidget(lblText);
lblText->setText(QString::fromStdString("test"));
QEventLoop ev;
splash.show();
QTimer::singleShot(5000, &ev, &QEventLoop::quit);
ev.exec();
//
// More code .. more event loops
//
return 0;
}
I have a QT main window and on top of this I want to add a widget ( containing buttons), as similar to image below.
If I add a dock widget , it is added in separate row, but not added as overlay on existing main window.
Any inputs ?
The easiest is to set your overlay widget's parent to be the main window. But because it will not be in any layout you have to take care of its geometry yourself. In case you want to have multiple overlays, the last added will be the top most.
#include <QApplication>
#include <QtGui>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QMainWindow *window = new QMainWindow();
QWidget *centralWiddget = new QWidget();
window->setCentralWidget(centralWiddget);
QVBoxLayout *layout = new QVBoxLayout(centralWiddget);
QPushButton *button = new QPushButton("Button in a layout");
layout->addWidget(button);
QPushButton *overlayButton = new QPushButton("Button overlay");
overlayButton->setParent(window);
overlayButton->setGeometry(40, 40, 120, 30)
window->show();
return app.exec();
}
You should look into using QStackedLayout to so this.
Following simplified code snippet:
#include <QtGui>
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QWidget *window = new QWidget();
QFormLayout *form = new QFormLayout();
// first row
form->addRow("First row:", new QLineEdit());
// second row
QWidget *hbox_widget = new QWidget();
QBoxLayout *hbox = new QHBoxLayout();
hbox->addWidget(new QLineEdit());
hbox_widget->setLayout(hbox);
form->addRow("Second row:", hbox_widget);
window->setLayout(form);
window->show();
return app.exec();
}
Here, I create a QFormLayout with two rows: the first is a classic example of label+input widget, the second one is almost the same, with the exception that the input widget is "boxed" in a QHBoxLayout(+containing widget).
For some reason, this seems to screw up the "vertical centering" of the input widget (in the 2nd row). How can I add a widget which contains others in a hboxlayout to a formlayout, and still have them all centered vertically?
Set the contents margins of the QHBoxLayout to 0:
QBoxLayout *hbox = new QHBoxLayout();
hbox->setContentsMargins(0, 0, 0, 0);
I have a QGridLayout with a QSplitter on it. In that QSplitter I have two elements with a splitter that lets me move the splitter from left to right. Fine, there it's fine. But then I want to add another splitter but that moves up to down. (I'll explain with an image.)
So it's mostly having 2 splitters, one that moves left-to-right and other that moves up-to-down.
I hope you understand.
QGridLayout *layout = new QGridLayout(this);
QSplitter *splitter = new QSplitter();
text1 = new QPlainTextEdit();
text2 = new QPlainTextEdit();
splitter->addWidget(text1);
splitter->addWidget(text2);
text1->resize(800, this->height());
layout->addWidget(splitter, 1, 0);
browser = new QTextBrowser();
browser->resize(1, 1);
layout->addWidget(browser, 2, 0);
setLayout(layout);
Here i add only 1 splitter, since i don't know how to do the 2nd one.
You should be able to adapt this for your needs easily. The idea is to create a container for the first two elements, then connect the container with the 3rd element all via splitters.
#include <QtGui>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget wnd;
QTextEdit *editor1 = new QTextEdit;
QTextEdit *editor2 = new QTextEdit;
QTextEdit *editor3 = new QTextEdit;
QSplitter *split1 = new QSplitter;
QSplitter *split2 = new QSplitter;
QVBoxLayout *layout = new QVBoxLayout;
QWidget *container = new QWidget;
QVBoxLayout *container_layout = new QVBoxLayout;
split1->addWidget(editor1);
split1->addWidget(editor2);
container_layout->addWidget(split1);
container->setLayout(container_layout);
split2->setOrientation(Qt::Vertical);
split2->addWidget(container);
split2->addWidget(editor3);
layout->addWidget(split2);
wnd.setLayout(layout);
wnd.show();
return app.exec();
}