I added a QLabel inside my widget and it is editable through UI, able to set new text also, but not able to retrieve the updated text using function text();
QLabel *m_ColorName = new QLabel("_________");
m_ColorName->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextEditable);
In UI i am able to edit to new text but by calling function
m_ColorName->text();
Its giving default txt _________
Code
H file
class CascadeColorHighlightWidgetItem : public QWidget
{
Q_OBJECT
public:
CascadeColorHighlightWidgetItem(int jobID, QColor selectedColor, QWidget *parent = 0);
int getJobID();
QString getSelectedColor();
QString getColorText();
private:
QString m_titleName;
QRectF m_textRect;
QVBoxLayout *m_mainLyt;
QLineEdit *m_pTitleEditor;
QLabel *m_ColorName;
QColor m_SelectedColor;
};
Source File
CascadeColorHighlightWidgetItem::CascadeColorHighlightWidgetItem(QColor selectedColor, QWidget *parent)
: QWidget(parent),m_titleName("______"),m_SelectedColor(selectedColor)
{
setFixedHeight(40);
setContentsMargins(0,0,0,0);
setFocusPolicy(Qt::StrongFocus);
m_pTitleEditor = new QLineEdit();
m_ColorName = new QLabel("_________");
m_ColorName->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextEditable);
QFont font( "Calibri" );
font.setPixelSize(14);
m_ColorName->setFont(font);
QPixmap pixmap;
pixmap.fill(QColor("transparent"));
QWidget *pixMap = new QWidget;
pixMap->resize(100,100);
QString styl = QString("background-color: rgb(%1, %2, %3)").arg(QString::number(m_SelectedColor.red()),
QString::number(m_SelectedColor.green()),
QString::number(m_SelectedColor.blue()));
pixMap->setStyleSheet(styl);
m_ColorToStringMap.insert(m_ColorName->text(),m_SelectedColor);
QHBoxLayout * mnLyt = new QHBoxLayout(this);
mnLyt->addWidget(pixMap);
mnLyt->addWidget(m_ColorName);
mnLyt->addSpacerItem(new QSpacerItem(30, 0, QSizePolicy::Minimum, QSizePolicy::Minimum));
int width = pixMap->width();
int height = pixMap->height();
int side = qMin(width, height);
QRegion maskedRegion(width/2- side/2, height/2- side/2, 20,
20, QRegion::Ellipse);
pixMap->setMask(maskedRegion);
}
QString CascadeColorHighlightWidgetItem::getColorText()
{
qDebug() << "!!!CascadeColorHighlightWidgetItem::getColorText" << m_ColorName->text(); // returns "_________"
return m_ColorName->text();
}
It seems that although the text is editable, the data is not updated in the text, that can be solved with a hack, we use findChild to find the QTextDocument that is created when the flags are activated and get the text of that element:
QTextDocument *td = m_ColorName->findChild<QTextDocument *>();
if(td){
QString text = td->toRawText(); // >= Qt 5.9
// td->toPlainText();
// td->toHtml();
}
Example:
#include <QApplication>
#include <QLabel>
#include <QTextDocument>
#include <QTimer>
#include <QVBoxLayout>
#include <QWidget>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget widget;
QLabel label("_________");
label.setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextEditable);
widget.setLayout(new QVBoxLayout);
widget.layout()->addWidget(&label);
QTimer timer;
QObject::connect(&timer, &QTimer::timeout, [&label](){
QTextDocument *td = label.findChild<QTextDocument*>();
if(td){
qDebug()<<td->toRawText();
//qDebug()<<td->toPlainText();
//qDebug()<<td->toHtml();
}
});
timer.start(100);
widget.show();
return a.exec();
}
Output:
[...]
"________"
"________"
"_______"
"____f___"
"____f___"
"____ff___"
"____fff___"
"____fff___"
"____ffff___"
"____fffff___"
"____fffff___"
[...]
Related
I have written a simple program where the dialog window opens in maximized mode and there are some labels and buttons set to this window. I set a gridlayout to my window and added the labels and buttons to the layout , and my code worked fine. When I try to use scrollarea instead of the pink label(which is in row=1 and column=1), I don't get the same result that I got from putting the pink label in (row=1 and column=1).
I know that when labels or buttons are supposed to be in a scrollarea, I must declare a widget and setwidget to the scrollarea, then declare a layout and setlayout to the widget. But for this case(that the scrollarea itself must be in a layout) I don't know what to do.
Here is the code:
dialog.h:
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QLabel>
#include <QPushButton>
#include <QScrollArea>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
QLabel *red_label;
QLabel *pink_label;
QLabel *yellow_label;
QScrollArea *scrollArea;
QPushButton *button_one;
QPushButton * button_two;
QPushButton * button_three;
private:
Ui::Dialog *ui;
};
#endif // DIALOG_H
dialog.cpp:
#include "dialog.h"
#include "ui_dialog.h"
#include <QLabel>
#include <QGridLayout>
#include <QWidget>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
this->setWindowState(this->windowState() ^ Qt::WindowMaximized);
this->setWindowFlags(windowFlags() | Qt::WindowMinimizeButtonHint |
Qt::WindowMaximizeButtonHint);
red_label = new QLabel(this);
red_label->showFullScreen();
red_label->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
red_label->setLayoutDirection(Qt::RightToLeft);
red_label->setObjectName("forlumha_Name");
red_label->setStyleSheet(
"QLabel#forlumha_Name {"
"background-color:red;"
"}"
);
/* pink_label = new QLabel(this);
pink_label ->showFullScreen();
pink_label ->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
pink_label ->setLayoutDirection(Qt::RightToLeft);
pink_label ->setObjectName("forlumha_Name");
pink_label ->setStyleSheet(
"QLabel#forlumha_Name {"
"background-color:pink;"
"}"
);*/
yellow_label = new QLabel(this);
yellow_label->showFullScreen();
yellow_label->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
yellow_label->setLayoutDirection(Qt::RightToLeft);
yellow_label->setObjectName("forlumha_Name");
yellow_label->setStyleSheet(
"QLabel#forlumha_Name {"
"background-color:yellow;"
"}"
);
button_one = new QPushButton(this);
button_one->setFixedSize(50,50);
button_two = new QPushButton(this);
button_two->setFixedSize(50,50);
button_three = new QPushButton(this);
button_three->setFixedSize(50,50);
//.......adding scrollarea........//
scrollArea = new QScrollArea(this);
//..........set layouts......//
QGridLayout *layout = new QGridLayout();
this->setLayout( layout );
layout->addWidget(red_label,0,0,15,1);
layout->addWidget(yellow_label,0,1,1,1);
//layout->addWidget(pink_label,1,1,14,1);
layout->addWidget(scrollArea,1,1,14,1);
layout->addWidget(button_one,6,2,1,1);
layout->addWidget(button_two,7,2,1,1);
layout->addWidget(button_three,8,2,1,1);
}
Dialog::~Dialog()
{
delete ui;
}
main.cpp:
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
The above code output :
When I comment the pink_label parts and declare a scrollArea and put it in row=1 and column=1,the output is :
I am doing a qt project, my goal is to draw something based on my input. The signal is in the main, and the drawing is in another object, I am not able to connect them.
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Frequency fre; //this is the class that does the drawing
QWidget *window = new QWidget;
QGridLayout *grid = new QGridLayout;
QGroupBox *groupBox = new QGroupBox(QObject::tr("Volume"));
QSpinBox *spinBox = new QSpinBox;
spinBox->setRange(0, 5);
QObject::connect(spinBox, SIGNAL(valueChanged(int)),&fre, SLOT(setVolume(int)));
QVBoxLayout *Vbox = new QVBoxLayout;
Vbox->addWidget(spinBox);
groupBox->setLayout(Vbox);
grid->addWidget(groupBox, 4,7,1,1);
window->setLayout(grid);
window->show();
return app.exec();
}
This is how I set up the Frequency class :
in h file
class Frequency : public QGLWidget
{
Q_OBJECT
public slots:
void setVolume(int value);
in cpp file
void Frequency :: setVolume(int val) {
vol = val;
updateGL();
}
void Frequency :: paintGL() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
draw();
}
Since I can't put all this in the comment section, I'm going to put it here:
I copied your code and I'm not able to reproduce the error with Qt version 5.14.0, because when I change the value in any way, the method, void MyObject::setVolume(int value) is being called.
This code should work for you. Possibly, you are running it in release mode, and used a breakpoint to check if it was called (which doesn't always work on release mode).
// #include "QtWidgetsApplication4.h"
#include <QtWidgets/QApplication>
#include "MyObject.h"
#include <QGridLayout>
#include <QGroupBox>
#include <QSpinBox>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// QtWidgetsApplication4 w;
// w.show();
// return a.exec();
MyObject object;
QWidget* window = new QWidget;
QGridLayout* grid = new QGridLayout;
QGroupBox* groupBox = new QGroupBox(QObject::tr("Volume"));
QSpinBox* spinBox = new QSpinBox;
spinBox->setRange(0, 5);
QObject::connect(spinBox, SIGNAL(valueChanged(int)), &object, SLOT(setVolume(int)));
QVBoxLayout* Vbox = new QVBoxLayout;
Vbox->addWidget(spinBox);
groupBox->setLayout(Vbox);
grid->addWidget(groupBox, 4, 7, 1, 1);
window->setLayout(grid);
window->show();
return a.exec();
}
#pragma once
#include <QObject>
class MyObject : public QObject
{
Q_OBJECT
public slots:
void setVolume(int value);
private:
int m_volume;
};
#include "MyObject.h"
void MyObject::setVolume(int value)
{
m_volume = value;
}
I have an application which shows some images in a grid and rest of the widgets in their respective positions. Something like this
Drawing inspiration from this i managed to solve keeping the aspect ratio of an image while resizing.
What are the issues:
The QLabel widgets(image widgets) overlap if QMainWindow is shrinked. Check the image below
The whole application is apparently not suited for different screens. Running this application on a laptop, layout is completely messed up.
What have i done:
Here is the MVCE code i have created
//ImageWidget.h
#ifndef IMAGEWIDGET_H
#define IMAGEWIDGET_H
#include <QLabel>
#include <QResizeEvent>
#include <QWidget>
class ImageWidget : public QLabel
{
Q_OBJECT
public:
explicit ImageWidget(QWidget* parent = nullptr);
virtual QSize sizeHint() const;
QPixmap scaledPixmap() const;
virtual int widthForHeight(int height) const;
public slots:
void setPixmap ( const QPixmap& p);
void resizeEvent(QResizeEvent* ev);
private:
QPixmap pix;
};
#endif // IMAGEWIDGET_H
ImageWidget.cpp
#include "imagewidget.h"
ImageWidget::ImageWidget(QWidget* parent) :
QLabel(parent)
{
setStyleSheet("QLabel{margin-left: 10px; border-radius: 25px; background: white; color: #4A0C46;}");
QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
sizePolicy.setWidthForHeight(true);
setSizePolicy(sizePolicy);
setMinimumSize(sizeHint());
}
void ImageWidget::setPixmap (const QPixmap& p)
{
pix = p;
QLabel::setPixmap(scaledPixmap());
}
/* virtual */ int ImageWidget::widthForHeight(int height) const
{
return pix.isNull() ? height * pix.height() / pix.width() : height;
}
QSize ImageWidget::sizeHint() const
{
if(pix.width() != 0)
{
int h = this->height();
return QSize(widthForHeight(h), h);
}
else
{
return QSize(300, 300);
}
}
QPixmap ImageWidget::scaledPixmap() const
{
auto scaled = pix.scaled(this->size() * devicePixelRatioF(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
scaled.setDevicePixelRatio(devicePixelRatioF());
return scaled;
}
void ImageWidget::resizeEvent(QResizeEvent* )
{
if (!pix.isNull())
{
QLabel::setPixmap(scaledPixmap());
}
}
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
#include <QTableWidget>
#include "imagewidget.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
protected:
virtual void resizeEvent(QResizeEvent* event) override;
private:
Ui::MainWindow *ui;
ImageWidget* lbl1;
ImageWidget* lbl2;
ImageWidget* lbl3;
ImageWidget* lbl4;
QPushButton* btn1;
QPushButton* btn2;
QPushButton* btn3;
QPushButton* btn4;
QTableWidget* tableWidget;
};
#endif // MAINWINDOW_H
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QVBoxLayout>
#include <QTabWidget>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QTabBar>
MainWindow::MainWindow(QWidget* parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QVBoxLayout* mainLayout = new QVBoxLayout;
QVBoxLayout* tabLay = new QVBoxLayout;
QHBoxLayout* buttonLay = new QHBoxLayout;
QGridLayout* gridLay = new QGridLayout;
QHBoxLayout* dockLay = new QHBoxLayout;
btn1 = new QPushButton(this);
btn1->setText("Button1");
btn2 = new QPushButton(this);
btn2->setText("Button2");
btn3 = new QPushButton(this);
btn3->setText("Button3");
btn4 = new QPushButton(this);
btn4->setText("Button4");
QTabWidget* tabView = new QTabWidget(this);
tabView->addTab(new QWidget(), "Table");
tabView->setMinimumSize(500, 300);
tabView->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
tableWidget = new QTableWidget(this);
tableWidget->setFixedHeight(200);
lbl1 = new ImageWidget(this);
lbl2 = new ImageWidget(this);
lbl3 = new ImageWidget(this);
lbl4 = new ImageWidget(this);
QPixmap lbl1Pix("1.png");
QPixmap lbl2Pix("2.png");
QPixmap lbl3Pix("3.png");
QPixmap lbl4Pix("4.png");
lbl1->setPixmap(lbl1Pix);
lbl1->show();
lbl2->setPixmap(lbl2Pix);
lbl2->show();
lbl3->setPixmap(lbl3Pix);
lbl3->show();
lbl4->setPixmap(lbl4Pix);
lbl4->show();
buttonLay->addWidget(btn1);
buttonLay->addWidget(btn2);
buttonLay->addWidget(btn3);
buttonLay->addWidget(btn4);
tabLay->addWidget(tabView);
gridLay->addWidget(lbl1, 0, 0);
gridLay->addWidget(lbl2, 0, 1);
gridLay->addWidget(lbl3, 1, 0);
gridLay->addWidget(lbl4, 1, 1);
dockLay->addLayout(gridLay);
dockLay->addLayout(tabLay);
mainLayout->addLayout(dockLay);
mainLayout->addLayout(buttonLay);
mainLayout->addWidget(tableWidget);
centralWidget()->setLayout(mainLayout);
setMinimumSize(200,200);
show();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::resizeEvent(QResizeEvent * /*event*/)
{
// get label dimensions
int h = lbl1->height();
int w = lbl1->widthForHeight(h);
lbl1->setFixedWidth(w);
lbl2->setFixedWidth(w);
lbl3->setFixedWidth(w);
lbl4->setFixedWidth(w);
}
Main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
What do i want:
Set a minimum size to the whole application so that the imagewidgets do not overlap.
Make the whole app work on any PC or laptop screen with different resolutions.
The imagewidgets should take up available space while keeping the aspect ratio of the images and the QTabWidget on the right should have a fixed size.
Maybe there is an easy solution but i am bit confused with Qt Layout management system.
EDIT1: Added the image with overlapping widgets
Here is what I think is happening. When you set the pixmap like here:
QLabel::setPixmap(scaledPixmap());
The label will set the size of the images as the minimum size. And from that point on the label can not be resized any smaller.
The solution I have found around this is to set the following resize flags for the QLabel in the constructor:
QSizePolicy sizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
setSizePolicy(sizePolicy);
This way the QLabel will be resizable at all times. Or you might have to adapt the minimum width.
I am not sure if that is even it, but maybe it is a good start. Let me know what you make of this.
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 >");
}
}