When I run it it will just finish right away and not show anything. I can't find anything wrong w/it and no one on #qt could either. I've got other apps working fine so I'm not sure. It's got something to do with the createForm call, if I omit that call in the constructor I do get a default QWidget displayed.
captchit.pro
#-------------------------------------------------
#
# Project created by QtCreator 2011-02-26T20:58:23
#
#-------------------------------------------------
QT += core gui network
TARGET = captchit
TEMPLATE = app
SOURCES += main.cpp\
widget.cpp
HEADERS += widget.h
main.cpp
#include "QtGui/QApplication"
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget mainWidget;
mainWidget.show();
return a.exec();
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
class QPixmap;
class QLabel;
class QLineEdit;
class QPushButton;
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
private slots:
void on_refreshButton_pressed();
void on_submitButton_pressed();
void on_closeButton_pressed();
private:
QPixmap captchaImage;
QLabel *imageLabel;
QLabel *statusLabel;
QLineEdit *captchaLineEdit;
QPushButton *submitButton;
QPushButton *refreshButton;
QPushButton *closeButton;
void createForm();
void createActions();
void getCaptcha();
void submitCaptcha();
};
endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "QtNetwork/QtNetwork"
#include "QtGui"
void Widget::on_refreshButton_pressed()
{
;
}
void Widget::on_submitButton_pressed()
{
;
}
void Widget::on_closeButton_pressed()
{
;
}
// Create UI Components
void Widget::createForm()
{
// Create Main Layout
QVBoxLayout *mainLayout = new QVBoxLayout(this);
// // set captcha pixmap to imageLabel for displaying
// imageLabel->setPixmap(captchaImage);
// Create Buttons
QVBoxLayout *buttonLayout = new QVBoxLayout();
submitButton->setText("Submit");
refreshButton->setText("Refresh");
closeButton->setText("Close");
buttonLayout->addWidget(submitButton);
buttonLayout->addWidget(refreshButton);
buttonLayout->addWidget(closeButton);
// Complete Layouts
// lineEdit & submitStatus
QVBoxLayout *lineEditLayout = new QVBoxLayout();
lineEditLayout->addStretch();
lineEditLayout->addWidget(statusLabel);
lineEditLayout->addWidget(captchaLineEdit);
lineEditLayout->addStretch();
// Create Bottom Layout
QHBoxLayout *bottomLayout = new QHBoxLayout();
bottomLayout->addLayout(lineEditLayout);
bottomLayout->addLayout(buttonLayout);
// Add to mainLayout
// mainLayout->addWidget(imageLabel);
mainLayout->addLayout(bottomLayout);
setLayout(mainLayout);
}
// Bind Slots and Signals
void Widget::createActions()
{
;
}
void Widget::getCaptcha()
{
;
}
void Widget::submitCaptcha()
{
;
}
Widget::Widget(QWidget *parent) :
QWidget(parent)
{
createForm();
// createActions();
}
You need to initialize your private class members, probably in your constructor, before you use them.
Widget::Widget(QWidget* parent) :
QWidget(parent)
{
captchaImage = new QPixmap;
imageLabel = new QLabel(this);
statusLabel = new QLabel(this);
captchaLineEdit = new QLineEdit(this);
submitButton = new QPushButton(this);
refreshButton = new QPushButton(this);
closeButton = new QPushButton(this);
createForm();
// createActions();
}
Also note that QPixmap does not derive from QObject, so you'll have to delete it manually. It may be better to remove the QPixmap *captchaImage member from your class and use temporary QPixmap objects in your code.
Oops, it was because I forgot to initialize all of my components, herp derp.
Related
I have a QListWidget that is in a QGridLayout in my constructor of the Draw class.QGridLAyout contains other elements.
Draw::Draw(QWidget *parent):QDialog(parent){
gridlayout=new QGridLayout;
gridlayout->addLayout(hbox,0,0);
gridlayout->addLayout(hbox1,1,0);
gridlayout->addLayout(hbox2,2,0);
gridlayout->addWidget(listWidget,3,0);
gridlayout->addLayout(hbox4,4,0);
this->setLayout(gridlayout);
}
When clicking on a QPushButton. I'm calling a slot that filled the QListWidget.
//SLOT
void Draw::fillListWidget(){
//item is QListWidgetItem
item=new QListWidgetItem;
item->setSizeHint(QSize(0,50));
listWidget->addItem(item);
//WidgetfillListWidget is an other class with parameters for the QListWidget
listWidget->setItemWidget(item,new WidgetfillListWidget(label1,path));
}
In my other class I build the new QWidget to fill the QlistWidget. The QListWidget contains a label, a string and a QPushButton.
WidgetfillListWidget::WidgetfillListWidget(QString label, QString p){
instanceDraw=new Draw(this);
QString name = label;
QString path = p;
name1=new QLabel(name,this);
path1=new QLineEdit(path,this);
remove=new QPushButton("remove",this);
hboxlist=new QHBoxLayout;
hboxlist->addWidget(name1);
hboxlist->addWidget(path1);
hboxlist->addWidget(remove);
setLayout(hboxlist);
connect(remove,SIGNAL(clicked()),instanceDraw,SLOT(removeItem()));
}
In the Draw class I have the slot that has to delete an item but it does not work
void Draw::removeItem(){
listWidget->takeItem(listWidget->row(item));
}
Deleting the item does not work. I think it comes from my Draw object in the connect. But I do not understand how to solve the problem. Does someone have an idea?
The problem is because the Draw created in WidgetfillListWidget is different from the original Draw, they are 2 different objects.
In this case the solution is to create a clicked signal in WidgetfillListWidget that is issued when the QPushButton is pressed.
Then that signal is connected to the removeItem() method. In it we must obtain the item, but that is not simple, one way to do it is through the geometric position as I show below:
widgetfilllistwidget.h
#ifndef WIDGETFILLLISTWIDGET_H
#define WIDGETFILLLISTWIDGET_H
#include <QWidget>
class QLabel;
class QPushButton;
class QHBoxLayout;
class QLineEdit;
class WidgetfillListWidget : public QWidget
{
Q_OBJECT
public:
explicit WidgetfillListWidget(const QString & name, const QString &path, QWidget *parent = nullptr);
signals:
void clicked();
private:
QLabel *lname;
QLineEdit *lpath;
QPushButton *premove;
QHBoxLayout *hboxlist;
};
#endif // WIDGETFILLLISTWIDGET_H
widgetfilllistwidget.cpp
#include "widgetfilllistwidget.h"
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
WidgetfillListWidget::WidgetfillListWidget(const QString &name, const QString & path, QWidget *parent) : QWidget(parent)
{
lname=new QLabel(name);
lpath=new QLineEdit(path);
premove=new QPushButton("remove");
hboxlist=new QHBoxLayout(this);
hboxlist->addWidget(lname);
hboxlist->addWidget(lpath);
hboxlist->addWidget(premove);
connect(premove, &QPushButton::clicked, this, &WidgetfillListWidget::clicked);
}
draw.h
#ifndef DRAW_H
#define DRAW_H
#include <QDialog>
class QGridLayout;
class QListWidget;
class QPushButton;
class Draw : public QDialog
{
Q_OBJECT
public:
Draw(QWidget *parent = 0);
~Draw();
private:
void fillListWidget();
void removeItem();
QGridLayout *gridlayout;
QListWidget *listWidget;
QPushButton *button;
};
#endif // DRAW_H
draw.cpp
#include "draw.h"
#include "widgetfilllistwidget.h"
#include <QGridLayout>
#include <QListWidget>
#include <QPushButton>
Draw::Draw(QWidget *parent)
: QDialog(parent)
{
button = new QPushButton("Press Me");
listWidget = new QListWidget;
gridlayout=new QGridLayout(this);
gridlayout->addWidget(listWidget, 0, 0);
gridlayout->addWidget(button, 1, 0);
connect(button, &QPushButton::clicked, this, &Draw::fillListWidget);
}
void Draw::fillListWidget()
{
QListWidgetItem *item=new QListWidgetItem;
item->setSizeHint(QSize(0,50));
listWidget->addItem(item);
//WidgetfillListWidget is an other class with parameters for the QListWidget
QString label = "label1";
QString path = "label2";
WidgetfillListWidget *widget = new WidgetfillListWidget(label,path);
listWidget->setItemWidget(item, widget);
connect(widget, &WidgetfillListWidget::clicked, this, &Draw::removeItem);
}
void Draw::removeItem()
{
WidgetfillListWidget *widget = qobject_cast<WidgetfillListWidget *>(sender());
if(widget){
QPoint gp = widget->mapToGlobal(QPoint());
QPoint p = listWidget->viewport()->mapFromGlobal(gp);
QListWidgetItem *item = listWidget->itemAt(p);
item = listWidget->takeItem(listWidget->row(item));
delete item;
}
}
Draw::~Draw()
{
}
I was trying to add a QWidget while runtime in Qt but It is showing SIGSEV signal received from OS because of segmentation fault.
Here is my code:
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLabel>
#include <QLineEdit>
#include <QVBoxLayout>
#include <QtGui>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_submit_clicked();
private:
Ui::MainWindow *ui;
QLabel *label;
QLineEdit *line_edit;
};
#endif // MAINWINDOW_H
//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_submit_clicked()
{
QString str = ui->lineEdit1->text();
QString str1 =ui->lineEdit2->text();
if(str=="rana"&&str1=="vivek")
{
label = new QLabel();
label->setText("Success");
MainWindow.layout->addWidget(label);
label->show();
}
else
{
line_edit = new QLineEdit();
line_edit->setText("Sorry");
MainWindow.layout->addWidget(line_edit);
line_edit->show();
}
}
//main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
I know that segmentation fault occurs due to dereferencing of a null pointer but i couldn't find where I have done that mistake.Any Suggestions?
MainWindow.layout->addWidget(label);
doesn't make a lot of sense - this should not even compile, as Sebastian noted.
First, make sure you have layout in the Ui file (I added one vertical layout named verticalLayout), so you have a layout where you will add widgets. You will have a pointer to it inside your ui object.
Now, just use addWidget on that layout and everything should work:
void MainWindow::on_pushButton_submit_clicked()
{
QString str = ui->lineEdit1->text();
QString str1 =ui->lineEdit2->text();
if(str=="rana"&&str1=="vivek")
{
QLabel *label = new QLabel();
label->setText("Success");
ui->verticalLayout->addWidget(label);
// label->show(); widgets will became the part of the MainWindow, as the addWidget
// will add them into the hierarchy.
}
else
{
QLineEdit *line_edit = new QLineEdit();
line_edit->setText("Sorry");
ui->verticalLayout->addWidget(line_edit);
// line_edit->show()
}
}
Note - addWidget will set the owner of the widget to be the layout, so the widget will be deleted on the destruction of the layout.
Maybe implementing in this way will make sense?
void MainWindow::on_pushButton_submit_clicked()
{
QString str = ui->lineEdit1->text();
QString str1 =ui->lineEdit2->text();
QWidget *w = new QWidget(this);
QVBoxLayout *layout = new QVBoxLayout; // creates a vertical layout
if(str=="rana"&&str1=="vivek")
{
label = new QLabel(w);
label->setText("Success");
layout->addWidget(label);
}
else
{
line_edit = new QLineEdit(w);
line_edit->setText("Sorry");
layout->addWidget(line_edit);
}
w->setLayout(layout);
setCentralWidget(w);
}
UPDATE:
QMainWindow already has a predefined layout, so it was needless to introduce a new one. The code above creates an intermediate widget and construct it using its own layout. Than the widget set as a central widget in the MainWindow.
I have the following minimal example code given.
main.cpp:
#include <QApplication>
#include "qt.h"
int main(int argc, char** argv)
{
QApplication app(argc, argv);
MyDialog mainWin;
mainWin.show();
return app.exec();
}
qt.cpp:
#include <QLabel>
#include "qt.h"
void MyDialog::setupUi()
{
setCentralWidget(new QWidget);
mainLayout = new QVBoxLayout( centralWidget() );
centralWidget()->setLayout(mainLayout);
// show the add new effect channel button
QPushButton* newKnobBtn = new QPushButton("new", this );
connect( newKnobBtn, SIGNAL(clicked()), this, SLOT(addNewKnob()));
mainLayout->addWidget( newKnobBtn, 0, Qt::AlignRight );
containerWidget = new QWidget(this);
scrollArea = new QScrollArea(containerWidget);
mainLayout->addWidget(containerWidget);
scrollLayout = new QVBoxLayout(scrollArea);
scrollArea->setLayout(scrollLayout);
/*
QSizePolicy pol;
pol.setVerticalPolicy(QSizePolicy::Expanding);
setSizePolicy(pol);
*/
addNewKnob(); // to fit size initially
}
void MyDialog::addNewKnob()
{
scrollLayout->addWidget(new QLabel("Hello World", this));
/*
containerWidget->adjustSize();
adjustSize();
*/
}
qt.h:
#include <QMainWindow>
#include <QVBoxLayout>
#include <QScrollArea>
#include <QPushButton>
class MyDialog : public QMainWindow
{
Q_OBJECT
private slots:
void addNewKnob();
private:
void setupUi();
QVBoxLayout* mainLayout;
QScrollArea* scrollArea;
QVBoxLayout* scrollLayout;
QWidget* containerWidget;
public:
MyDialog( ) { setupUi(); }
};
Compiling: Put all in one directory, type
qmake -project && qmake && make
I have the adjustSize() solution from here, but it does not work: (link). Everything I commented out was things I tried but did not help.
How do I make containerWidget and scrollLayout grow correctly, when a new Label is being added to scrollLayout?
Here's a simplified version that works for me:
qt.cpp:
#include <QLabel>
#include <QPushButton>
#include <QScrollArea>
#include "qt.h"
MyDialog::MyDialog()
{
QWidget * mainWidget = new QWidget;
QBoxLayout * mainLayout = new QVBoxLayout(mainWidget);
setCentralWidget(mainWidget);
// show the add new effect channel button
QPushButton* newKnobBtn = new QPushButton("new");
connect( newKnobBtn, SIGNAL(clicked()), this, SLOT(addNewKnob()));
mainLayout->addWidget( newKnobBtn, 0, Qt::AlignRight );
QScrollArea * scrollArea = new QScrollArea;
scrollArea->setWidgetResizable(true);
mainLayout->addWidget(scrollArea);
QWidget * labelsWidget = new QWidget;
labelsLayout = new QVBoxLayout(labelsWidget);
scrollArea->setWidget(labelsWidget);
addNewKnob(); // to fit size initially
}
void MyDialog::addNewKnob()
{
labelsLayout->addWidget(new QLabel("Hello World"));
}
qt.h:
#include <QMainWindow>
#include <QBoxLayout>
class MyDialog : public QMainWindow
{
Q_OBJECT
public:
MyDialog( );
private slots:
void addNewKnob();
private:
QBoxLayout * labelsLayout;
};
You have containerWidget that contain only one QScrollArea. I don't know why do you need this. But if you need this for some reason, you need to add a layout to this widget in order to make layouts work. Also do not create a layout for QScrollArea. It already have internally implemented layout. You should add scrollLayout to the scroll area's viewport() widget instead.
When you construct a layout and pass a widget to its constructor, the layout is automatically assigned to the passed widget. You should not call setLayout after that. This action will take no effect and produce console warning.
Basically, I have multiple widgets I'm trying to switch between... and the default is a QTabWidget. Aside from some modification, the two examples (QStackedWidget and QTabsExapmle) are just mingled together. I can't get the "connect" portion to work (get an error: no matching function for call to QTabsExample::connect), and nothing displays on screen unless stackedWidget->addWidget(tabWidget) is the first in the list (and even then, I only see the view in the upper left hand corner).
QTabsExample.h
#ifndef QTABSEXAMPLE_H
#define QTABSEXAMPLE_H
#include <QtGui/QMainWindow>
#include <QtGui/QScrollArea>
#include <QtGui/QFrame>
#include <QtGui/QVBoxLayout>
#include <QtGui/QPushButton>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QGroupBox>
#include <QtGui/QFormLayout>
#include <QtGui/QMessageBox>
#include <QtCore/QPointer>
#include <QtCore/QFile>
#include <QtCore/QIODevice>
#include <QtCore/QList>
#include <QtCore/QMap>
#include <QtCore/QString>
#include <QtXml/QXmlStreamReader>
#include <QtDebug>
#include <QBool>
#include <QSignalMapper>
#include <QStackedLayout>
class QTabsExample: public QMainWindow {
Q_OBJECT
public:
QTabsExample(QWidget *parent = 0);
~QTabsExample();
private:
void buildTabMenuBar(int index);
public slots:
void activeTabChanged(int index);
void setCurrentIndex(int);
signals:
void activated(int);
private:
QTabWidget* tabWidget;
QWidget* customWidget1;
QWidget* customWidget2;
QWidget* customWidget3;
};
// ---------------------------------------------------------------------------
// QMyWidget - Tab1
//
class QMyWidget: public QWidget {
Q_OBJECT
public:
QMyWidget(QWidget *parent = 0);
~QMyWidget();
public slots:
void runOnTabSelect();
private:
QPointer<QVBoxLayout> _layout;
};
// ---------------------------------------------------------------------------
// QMyWidget2 - Tab2
//
class QMyWidget2: public QWidget {
Q_OBJECT
public:
QMyWidget2(QWidget *parent = 0);
~QMyWidget2();
public slots:
void runOnTabSelect();
};
// ---------------------------------------------------------------------------
// QMyWidget3 - Tab3
//
class QMyWidget3: public QWidget {
Q_OBJECT
public:
QMyWidget3(QWidget *parent = 0);
~QMyWidget3();
public slots:
void runOnTabSelect();
private:
QPointer<QVBoxLayout> _layout;
};
#endif // QTABSEXAMPLE_H
QTabsExample.cpp
#include <QtGui>
#include <QApplication>
#include "QTabsExample.h"
QTabsExample::QTabsExample(QWidget *parent) : QMainWindow(parent) {
setContextMenuPolicy(Qt::NoContextMenu);
this->setWindowTitle("Main Window, I think");
QStackedLayout *stackedLayout = new QStackedLayout;
//create tab widget
QTabWidget *tabWidget = new QTabWidget();
tabWidget->setContextMenuPolicy(Qt::NoContextMenu);
QObject::connect(tabWidget, SIGNAL(currentChanged(int)),this, SLOT(activeTabChanged(int)));
QMyWidget* widget1 = new QMyWidget();
tabWidget->addTab(widget1, "Tab1");
QMyWidget2* widget2 = new QMyWidget2();
tabWidget->addTab(widget2, "Tab2");
QMyWidget3* widget3 = new QMyWidget3();
tabWidget->addTab(widget3, "Tab3");
//set programatically
tabWidget->setStyleSheet("QTabBar::tab { height: 70px; width: 80px; font-size: 15px;}");
//create other widgets
QWidget* customWidget1 = new QWidget;
QWidget* customWidget2 = new QWidget;
QWidget* customWidget3 = new QWidget;
//add layouts to widgets
customWidget1->setContextMenuPolicy(Qt::NoContextMenu);
customWidget2->setContextMenuPolicy(Qt::NoContextMenu);
customWidget3->setContextMenuPolicy(Qt::NoContextMenu);
customWidget1->setWindowTitle("Widget 1");
customWidget2->setWindowTitle("Widget 2");
customWidget3->setWindowTitle("Widget 3");
//insert content to make sure it's viewable
QPalette palette;
palette.setBrush(this->backgroundRole(), QBrush(QImage("c://default.png")));
customWidget1->setPalette(palette);
palette.setBrush(this->backgroundRole(), QBrush(QImage("c://default2.png")));
customWidget2->setPalette(palette);
palette.setBrush(this->backgroundRole(), QBrush(QImage("c://default3.png")));
customWidget3->setPalette(palette);
//add widgets to stack
stackedLayout->addWidget(tabWidget);
stackedLayout->addWidget(customWidget1);
stackedLayout->addWidget(customWidget2);
stackedLayout->addWidget(customWidget3);
QComboBox *pageComboBox = new QComboBox;
pageComboBox->addItem(tr("Tab Page"));
pageComboBox->addItem(tr("page 2"));
pageComboBox->addItem(tr("page 3"));
pageComboBox->addItem(tr("page 4"));
connect(pageComboBox, SIGNAL(activated(int))), stackedLayout, SLOT(setCurrentIndex(int));
QVBoxLayout *_layout = new QVBoxLayout;
_layout->addWidget(pageComboBox);
_layout->addLayout(stackedLayout);
setLayout(_layout);
//setCentralWidget(tabWidget);
#ifdef Q_OS_SYMBIAN
QWidgetList widgets = QApplication::allWidgets();
QWidget* w = 0;
foreach(w,widgets) {
w->setContextMenuPolicy(Qt::NoContextMenu);
}
#endif
}
QTabsExample::~QTabsExample(){
}
void QTabsExample::activeTabChanged(int index) {
buildTabMenuBar(index);
}
void QTabsExample::buildTabMenuBar(int index) {
QMenuBar* menubar = menuBar();
menubar->clear();
switch (index) {
case 0:
{
menubar->addAction("", tabWidget->widget(index), SLOT(runOnTabSelect()));
break;
}
case 1:
{
menubar->addAction("", tabWidget->widget(index), SLOT(runOnTabSelect()));
break;
}
case 2:
{
menubar->addAction("", tabWidget->widget(index), SLOT(runOnTabSelect()));
break;
}
default:
{
break;
}
};
}
//tab1
QMyWidget::QMyWidget(QWidget *parent) : QWidget(parent) {
setContextMenuPolicy(Qt::NoContextMenu);
QVBoxLayout* _layout = new QVBoxLayout(this);
//buttons get created
this->setLayout(_layout);
}
QMyWidget::~QMyWidget() {
}
void QMyWidget::runOnTabSelect() {
}
//tab2
QMyWidget2::QMyWidget2(QWidget *parent) : QWidget(parent) {
setContextMenuPolicy(Qt::NoContextMenu);
QVBoxLayout* _layout = new QVBoxLayout(this);
//buttons get created
this->setLayout(_layout);
}
QMyWidget2::~QMyWidget2() {
}
void QMyWidget2::runOnTabSelect() {
}
//tab3
QMyWidget3::QMyWidget3(QWidget *parent) : QWidget(parent) {
setContextMenuPolicy(Qt::NoContextMenu);
QVBoxLayout* _layout = new QVBoxLayout(this);
//buttons get created
this->setLayout(_layout);
}
QMyWidget3::~QMyWidget3() {
}
void QMyWidget3::runOnTabSelect() {
}
main.cpp
#include "QTabsExample.h""
#include <QtGui>
#include <QApplication>
#include <QtGui/QApplication>
#include <QPixmap>
#include <QWidget>
#include <QMainWindow>
#include <QSplashScreen>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTabsExample w;
w.showMaximized();
return a.exec();
}
Well for starters you need to uncomment //QTabsExample w; in main.cpp, but I guess, that is just a relict from experimenting...
Also, what do you mean by "nothing displays on screen."?
Do you mean, that an empty window pops up? Or does no window at all open? Or does the screen turn black?
Have you eliminated the possibility that the "app.css" file might be corrupt?
Have you tried deleting the Makefiles and moc_* files?
EDIT:
I tried to simplify the constructor of QTabsExample
#include <QtGui>
#include <QApplication>
#include "QTabsExample.h"
QTabsExample::QTabsExample(QWidget *parent) : QMainWindow(parent) {
setContextMenuPolicy(Qt::NoContextMenu);
this->setWindowTitle("Main Window, I think");
//create tab widget
QTabWidget *tabWidget = new QTabWidget();
tabWidget->setContextMenuPolicy(Qt::NoContextMenu);
//QObject::connect(tabWidget, SIGNAL(currentChanged(int)),this, SLOT(activeTabChanged(int))); // I have no experience with mobile developement
// can you manipulate the menubar on a mobile device?
QMyWidget* widget1 = new QLabel(tr("Widget1")); // simplification
tabWidget->addTab(widget1, "Tab1");
QMyWidget2* widget2 = new QLabel(tr("Widget2"));
tabWidget->addTab(widget2, "Tab2");
QMyWidget3* widget3 = new QLabel(tr("Widget3"));
tabWidget->addTab(widget3, "Tab3");
//set programatically
tabWidget->setStyleSheet("QTabBar::tab { height: 70px; width: 80px; font-size: 15px;}");
//create other widgets
QWidget* customWidget1 = new QLabel(tr("Hello1")); // simplification
QWidget* customWidget2 = new QLabel(tr("Hello2"));
QWidget* customWidget3 = new QLabel(tr("Hello3"));
// create stacked layout (closer to where it is actually used)
QStackedLayout *stackedLayout = new QStackedLayout;
//add widgets to stack
stackedLayout->addWidget(tabWidget);
stackedLayout->addWidget(customWidget1);
stackedLayout->addWidget(customWidget2);
stackedLayout->addWidget(customWidget3);
QComboBox *pageComboBox = new QComboBox;
pageComboBox->addItem(tr("Tab Page"));
pageComboBox->addItem(tr("page 2"));
pageComboBox->addItem(tr("page 3"));
pageComboBox->addItem(tr("page 4"));
connect(pageComboBox, SIGNAL(activated(int))), stackedLayout, SLOT(setCurrentIndex(int));
QVBoxLayout *_layout = new QVBoxLayout;
_layout->addWidget(pageComboBox);
_layout->addLayout(stackedLayout);
setLayout(_layout);
//setCentralWidget(tabWidget);
#ifdef Q_OS_SYMBIAN
QWidgetList widgets = QApplication::allWidgets();
QWidget* w = 0;
foreach(w,widgets) {
w->setContextMenuPolicy(Qt::NoContextMenu);
}
#endif
}
Expected behaviour:
a combo box on the top with choices: "Tab Page", "page 2", ...
below a QTabWidget witht the tabs: "Tab1", "Tab2", "Tab3"
Tab1 should be displayed with a QLabel, which says "Widget1"
when you select another tab, the label should say "WidgetX" // depending on your choice
when you select another widget from the combo box you should see "HelloX" // depending on your choice
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 >");
}
}