I developed iPhone application in Qt Widget on MAC, but problem with size of screen and all elements, I set all elements and screen for iPhone 4s. When this application run on iPhone 5, then all things look very smaller. So I want to set size of screen and all elements so It looks better in all type of phones and screen.
In widget application I cannot add directly in .qml file, I can change by drag and drop only.
Thanks in advanace.
You could set the screen size dynamically by querying it from QDesktopWidget.
QRect r = QApplication::desktop()->screenGeometry();
int h = r.height();
int w = r.width();
The Qt layout system provides a simple and powerful way of automatically arranging child widgets within a widget to ensure that they make good use of the available space.
See the example code below which takes the full screen and splits that space for text label and button. You can resize the window until the widgets' minimum sizes limit.
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include <QVBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QApplication>
#include <QDesktopWidget>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
QWidget *central_widget = new QWidget;
QVBoxLayout *layout = new QVBoxLayout(central_widget);
QPushButton *button1 = new QPushButton("Button1");
layout->addWidget(button1);
QLabel *label1 = new QLabel();
label1->setText("Label1");
label1->setAlignment(Qt::AlignCenter);
layout->addWidget(label1);
setCentralWidget(central_widget);
QRect r = QApplication::desktop()->screenGeometry();
int h = r.height();
int w = r.width();
button1->setMinimumHeight(h/4);
button1->setMaximumHeight(h/2);
button1->setMinimumWidth(w/2);
button1->setMaximumWidth(w);
label1->setMinimumHeight(h/4);
label1->setMaximumHeight(h/2);
label1->setMinimumWidth(w/2);
label1->setMaximumWidth(w);
resize(w, h);
}
MainWindow::~MainWindow()
{
}
Related
I have a very simple window with a QGraphicsView, a QGraphicsScene inside, and a simple QPushButton. When user clicks button, a line should be added to the scene. However, since I set QGraphicsView::NoViewportUpdate, the line shouldn't be displayed. On the opposite, the line gets displayed.
According to the documentation, QGraphicsView will never update its viewport when the scene changes; the user is expected to control all updates. This mode disables all (potentially slow) item visibility testing in QGraphicsView, and is suitable for scenes that either require a fixed frame rate, or where the viewport is otherwise updated externally.
How do I solve this problem?
Here is the code:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QWidget>
#include <QPushButton>
class MainWindow : public QWidget
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
QGraphicsView* view;
QGraphicsScene* scene;
QPushButton* b;
public slots:
void start();
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include <QVBoxLayout>
MainWindow::MainWindow(QWidget *parent)
: QWidget(parent)
{
scene = new QGraphicsScene(0, 0, 400, 400);
view = new QGraphicsView(scene);
view->setViewportUpdateMode(QGraphicsView::NoViewportUpdate);
b = new QPushButton("Start");
connect (b, &QPushButton::clicked, this, &MainWindow::start);
QVBoxLayout* layout = new QVBoxLayout;
layout->addWidget(view);
layout->addWidget(b);
setLayout(layout);
}
MainWindow::~MainWindow()
{
}
void MainWindow::start()
{
scene->addLine(0, 0, 200, 200);
}
I "solved" that. I discovered the viewport doesn't get updated if you do NOT hover (for example) it with the mouse. So, if you do not interact, viewport does not update. However, viewport does not update if you scroll with the mouse wheel inside the qGraphicsView.
How do I change the QWidget inside a tab of a QTabWidget, knowing only the tab index?
void MainWindow::on_toolButton_2_clicked()
{
TextItem myitem = new TextItem;//is a class TextItem : public QWidget
int tabindex = 2;
ui->tabwidget1->//i don't have a idea to change widget of a Tab by tab index
}
It's hard to say what solution would best suit your problem since you don't explain much of it.
A first approach would be to wrap the content of each tab inside a container QWidget: when you want to change the content of one tab, you just have to change the content of the container QWidget.
Another approach would be to delete the tab with the old content and create a new one with the new content.
EDIT:
Here is a quick implementation of the first approach I mentioned above:
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
void buildTabWidget();
private slots:
void changeTabContent() const;
private:
QTabWidget* tab_widget;
};
#endif // MAINWINDOW_H
mainwindow.cpp:
#include "mainwindow.h"
#include <QLabel>
#include <QLayout>
#include <QPushButton>
#include <QTabWidget>
void MainWindow::buildTabWidget()
{
// The container will hold the content that can be changed
QWidget *container = new QWidget;
tab_widget = new QTabWidget(this);
tab_widget->addTab(container, "tab");
// The initial content of the container is a blue QLabel
QLabel *blue = new QLabel(container);
blue->setStyleSheet("background: blue");
blue->show();
}
void MainWindow::changeTabContent() const
{
// retrieve the QWidget 'container'
QWidget *container = tab_widget->widget(0);
// the 'blue' QLabel
QWidget *old_content = dynamic_cast<QWidget*>(container->children()[0]);
delete old_content;
// create a red QLabel, as a new content
QWidget *new_content = new QLabel(container);
new_content->setStyleSheet("background: red");
new_content->show();
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
buildTabWidget();
QPushButton* push_button = new QPushButton("Change content");
connect(push_button, SIGNAL(clicked(bool)), this, SLOT(changeTabContent()));
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(tab_widget);
layout->addWidget(push_button);
QWidget *window = new QWidget();
window->setLayout(layout);
window->show();
setCentralWidget(window);
}
Clicking the button Change content will delete the old content (the blue QLabel) in the tab, and will replace it by creating a new content (a red QLabel):
I am a QT newbie and trying to play around with Apps. I have just coded a very trivial App with some buttons. The main idea is to have a small "logo" in my App. LAter I would like to add some background image as well.
I have coded from an example App with a grid layout within which is a QBoxLayout which groups my buttons.
As you can see in my code, I have tried adding the Logo everywhere. When I added it in main.cpp, I have two views one showing buttons and the other my logo. Of course I do not want this. So I tried adding it in mainwindow.cpp but in this case, I don't see my Logo appearing anywhere at all :(
Please advise.
Here is the code:
main.cpp:
#include <QtGui/QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsPixmapItem>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Window window;
window.show();
/*
QGraphicsScene scene;
QGraphicsView view(&scene);
QGraphicsPixmapItem item(QPixmap("/home/marc/Desktop/Niranjana/Images/logo.9.png"));
scene.addItem(&item);
view.show();
*/
return a.exec();
}
mainwindow.h
#ifndef WINDOW_H
#define WINDOW_H
#include <QWidget>
#include <QRadioButton>
class QGroupBox;
class Window : public QWidget
{
Q_OBJECT
public:
Window(QWidget *parent = 0);
void onCheck_remote(int flag);
void onCheck_local(int flag);
private:
QRadioButton *button_local;
QRadioButton *button_remote;
QGroupBox *createPushButtonGroup();
};
#endif
mainwindow.cpp
#include <QtGui>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsPixmapItem>
#include "mainwindow.h"
Window::Window(QWidget *parent)
: QWidget(parent)
{
QGridLayout *grid = new QGridLayout;
QGraphicsScene scene;
QGraphicsPixmapItem item(QPixmap("/home/test/logo.png"));
QGraphicsView view(&scene);
scene.addItem(&item);
view.show();
grid->addWidget(view.viewport(), 1, 1);
grid->addWidget(createPushButtonGroup(), 2, 1);
setLayout(grid);
setWindowTitle(tr("My App"));
resize(480, 420);
}
QGroupBox *Window::createPushButtonGroup()
{
QGroupBox *groupBox = new QGroupBox();
/*
QGraphicsScene scene;
QGraphicsPixmapItem item(QPixmap("/home/marc/Desktop/Niranjana/Images/logo.9.png"));
QGraphicsView view(&scene);
scene.addItem(&item);
scene.setBackgroundBrush(Qt::white);
view.show();
*/
QPushButton *button1 = new QPushButton(tr("&Start"));
QPushButton *button2 = new QPushButton(tr("&Stop"));
button_local = new QRadioButton(tr("&with power"));
button_remote = new QRadioButton(tr("without power"));
button_local->setChecked(1);
QVBoxLayout *vbox = new QVBoxLayout;
// vbox->addSpacing(10);
// vbox->addWidget(view.viewport());
//vbox->addSpacing(10);
vbox->addWidget(button1);
vbox->addSpacing(10);
vbox->addWidget(button2);
vbox->addSpacing(50);
vbox->addWidget(button_local);
vbox->addWidget(button_remote);
vbox->addStretch(1);
groupBox->setLayout(vbox);
return groupBox;
}
You should
Make your scene, view, item pointer members of your window class
Instantiate them in your Window ctor
do grid->addWidget(view, 1, 1);
Otherwise, all the items instantiated on the stack in the ctor will get deleted when exiting.
I have a problem with a QGridLayout. One row of my layout contains an element (QProgressbar) that is normaly hidden. When there is some progress to report i call show on it. The problem is that when i call show on the QProgressbar the row above the row containing it will be slightly resized in height (1-3 px). So the whole layout does a little "jump" which looks ugly.
I have given a minimalRowHeight to the row that contains the QProgressbar that is much larger then the height of the QProgressbar but still the height of the row will increase on show().
I have attached a very minimal version of my program that demonstrates the problem. Can anyone give me a hint what is going on there?
Header:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui/QMainWindow>
#include <QLineEdit>
#include <QtWebKit/QWebView>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
private:
QLineEdit* input;
QWebView *webview;
private slots:
void slotLoadButton();
};
#endif // MAINWINDOW_H
Source:
#include "mainwindow.h"
#include <QProgressBar>
#include <QPushButton>
#include <QGridLayout>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QGridLayout *grid = new QGridLayout;
input = new QLineEdit;
QPushButton *loadButton = new QPushButton("load");
connect(loadButton, SIGNAL(clicked()),
this, SLOT(slotLoadButton()));
webview = new QWebView;
QProgressBar *progress = new QProgressBar;
progress->setFixedHeight(25);
progress->hide();
connect(webview, SIGNAL(loadStarted()),
progress, SLOT(show()));
connect(webview, SIGNAL(loadProgress(int)),
progress, SLOT(setValue(int)));
connect(webview, SIGNAL(loadFinished(bool)),
progress, SLOT(hide()));
grid->addWidget(input, 0, 0);
grid->addWidget(loadButton, 0, 1);
grid->addWidget(webview, 1, 0, 1, -1);
grid->setRowMinimumHeight(2, 35);
grid->addWidget(progress, 2, 1);
QWidget* widget = new QWidget;
widget->setLayout(grid);
setCentralWidget(widget);
}
void MainWindow::slotLoadButton()
{
QUrl url = input->text();
webview->load(url);
}
This is likely caused by the vertical spacing and/or margins of the layout. You should try playing with those properties.
This looks like a bug in Qt. Try reporting it
This is a workaround:
//grid->addWidget(progress, 2, 1);
QHBoxLayout *l = new QHBoxLayout;
l->addWidget(progress);
QWidget *w = new QWidget;
w->setLayout(l);
grid->addWidget(w, 2, 1);
I am attempting to create a custom widget. My Widget renders itself unless it is inside a scroll area. The code below works. If I change the if(0) to an if(1) inside the MainWindow constructor, it will not render the "Hello World" string. I assume that I must (re)implement some additional methods, but so far I have not been able to find the correct ones with trial and error.
// hellowidget.h
#ifndef HELLOWIDGET_H
#define HELLOWIDGET_H
#include <QtGui>
class HelloWidget : public QWidget
{
Q_OBJECT
public:
HelloWidget(QWidget *parent = 0);
void paintEvent(QPaintEvent *event);
};
#endif // HELLOWIDGET_H
// hellowidget.cpp
#include "hellowidget.h"
HelloWidget::HelloWidget(QWidget *parent)
: QWidget(parent)
{
}
void HelloWidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.drawText(rect(), Qt::AlignCenter, "Hello World");
}
// mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
};
#endif // MAINWINDOW_H
// mainwindow.cpp
#include "mainwindow.h"
#include "hellowidget.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
HelloWidget *hello = new HelloWidget;
QWidget *central = hello;
if( 0 )
{
QScrollArea *scroll = new QScrollArea ;
scroll->setWidget(hello);
central = scroll;
}
setCentralWidget( central );
}
MainWindow::~MainWindow()
{
}
// main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
You just have to give your HelloWidget a size and place.
Add this line to your code.
hello->setGeometry(QRect(110, 80, 120, 80));
Or if you want to fill the scroll area with your widget:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QScrollArea *const scroll(new QScrollArea);
QHBoxLayout *const layout(new QHBoxLayout(scroll));
HelloWidget *const hello(new HelloWidget);
hello->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
layout->addWidget(hello);
setCentralWidget( scroll );
}
Per Qt docs, "When using a scroll area to display the contents of a custom widget, it is important to ensure that the size hint of the child widget is set to a suitable value. If a standard QWidget is used for the child widget, it may be necessary to call QWidget::setMinimumSize() to ensure that the contents of the widget are shown correctly within the scroll area."
Does it work right if you follow these instructions?
I was pulling my hair out over this also, but eventually found QScrollArea's setWidgetResizable, which made the QScrollArea allow my widget to expand to take up the available space.