the resizeEvent function does not work when I define the window with the size of the window, please help.
I tested this function with Main Window and UI Form and answered, but did not work in my code.
Thanks.
main.cpp
Keypad *keypad;
keypad = new Keypad();
keypad->Widget_Keypad->show();
Keypad.cpp
#include "keypad.h"
Keypad::Keypad()
{
CreateUi();
}
void Keypad::resizeEvent(QResizeEvent *e)
{
qDebug() << e->size().width();
}
void Keypad::CreateUi()
{
Widget_Keypad = new QWidget;
Vertical_Layout = new QVBoxLayout;
Hlayout_ShowRange = new QHBoxLayout;
lbl_ShowRange = new QLabel;
lbl_ShowRange->setText( "0 ~ 999" );
Hlayout_ShowRange->addWidget( lbl_ShowRange );
Vertical_Layout->addLayout( Hlayout_ShowRange );
Widget_Keypad->setLayout( Vertical_Layout );
}
Related
I am trying to implement a camera view app in Qt, but I don't know why It doesn't fix correctly layout / buttons. First, before to update the qlabel, I have this:
But when I click the start button. the GUI changes to this:
The code to create the window is:
cameraLabel = new QLabel(tr("No camera loaded"));
cameraLabel->setAlignment(Qt::AlignCenter);
cameraLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
cameraLabel->setBackgroundRole(QPalette::Dark);
cameraLabel->setAutoFillBackground(true);
createButtons();
...
..
.
mainLayout = new QHBoxLayout;
mainLayout->addWidget(cameraLabel);
mainLayout->addLayout(buttonsLayout);
setLayout(mainLayout);
void CameraPlayer::createButtons()
{
QSize iconSize(36, 36);
playButton = new QToolButton;
playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
playButton->setIconSize(iconSize);
playButton->setToolTip(tr("Play"));
connect(playButton, SIGNAL(clicked()), this, SLOT(play()));
snapshotButton = new QToolButton;
snapshotButton->setIcon(style()->standardIcon(QStyle::SP_DialogSaveButton));
snapshotButton->setIconSize(iconSize);
snapshotButton->setToolTip(tr("Snapshot"));
connect(snapshotButton, SIGNAL(clicked()), this, SLOT(snapshot()));
stopButton = new QToolButton; cameraLabel = new QLabel(tr("No camera loaded"));
cameraLabel->setAlignment(Qt::AlignCenter);
cameraLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
cameraLabel->setBackgroundRole(QPalette::Dark);
cameraLabel->setAutoFillBackground(true);
createButtons();
SParameters& globalParams = GlobalParameters::Instance().params;
globalParams.IPAddress = "169.251.0.1";
globalParams.isColor = true;
globalParams.timer_s =10;
timerIdCamera = 0;
mainLayout = new QHBoxLayout;
mainLayout->addWidget(cameraLabel);
mainLayout->addLayout(buttonsLayout);
setLayout(mainLayout);
stopButton->setIcon(style()->standardIcon(QStyle::SP_MediaStop));
stopButton->setIconSize(iconSize);
stopButton->setToolTip(tr("Stop"));
connect(stopButton, SIGNAL(clicked()), this, SLOT(stop()));
colorButton = new QPushButton;
colorButton->setText(tr("WB / Color"));
connect(colorButton, SIGNAL(clicked()), this, SLOT(changeColor()));
buttonsLayout = new QVBoxLayout;
buttonsLayout->addStretch();
buttonsLayout->addWidget(playButton);
buttonsLayout->addWidget(snapshotButton);
buttonsLayout->addWidget(stopButton);
buttonsLayout->addWidget(colorButton);
buttonsLayout->addStretch();
}
To udpate the qLabel,It is configured a timer / trigger which is called every x milsecs and update the qlabel object:
void CameraPlayer::timerEvent(QTimerEvent *event){
try
{
if (timerIdCamera!=0)
{
if (camera.CameraHasImage())
{
QImage *qImage = camera.CameraGrab();
if (qImage!=NULL && SIZE_IMAGE!=QString::number(qImage->byteCount()))
{
cameraLabel->setPixmap(QPixmap::fromImage(*qImage));
cameraLabel->setFixedWidth(qImage->width());
cameraLabel->show();
}
delete[] qImage->bits();
delete qImage;
}
}
}
catch (std::exception &ex)
{
QMessageBox qMBox;
qMBox.setText(QString::fromUtf8(ex.what()));
qMBox.exec();
}
}
The problem is here
cameraLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
I was ignoring layout policies from other widgets / layouts. I commented this and It started to work well.
I have a problem regarding a QWidget (custom made) inside a QMainWindow (custom made). My problem is that when I add my widget to my window as its central widget using setCentralWidget() method, it won't show the widgets background. It's important to show the background correctly.
Here is my MyWindow.cpp code:
#include "MyMainWindow.h"
MyMainWindow::MyMainWindow(QWidget * parent, Qt::WindowFlags flag) :
QMainWindow(parent, flag)
{
this->setFixedSize(1120, 630);
menu = new MyMenu(this);
// setting = new MySetting();
// tutorial = new MyTutorial();
// game = new MyGame();
this->setCentralWidget(menu);
this->show();
}
MyMainWindow::~MyMainWindow()
{
}
My MyMenu.cpp code:
#include "MyMenu.h"
MyMenu::MyMenu(QWidget *parent, Qt::WindowFlags f) :
QWidget(parent, f)
{
this->resize(1120, 630);
this->set_background();
this->construct_buttons();
this->construct_menu();
}
MyMenu::~MyMenu()
{
delete start;
delete setting;
delete tutorial;
delete exit;
delete buttons;
delete logo;
delete menu;
}
void MyMenu::construct_menu()
{
menu = new QVBoxLayout(this);
logo = new QLabel(this);
QPixmap *pixmap = new QPixmap("/home/kahrabian/ClionProjects/Shooter-AP93UT/Contents/logo.png");
logo->setPixmap(*pixmap);
logo->setAlignment(Qt::AlignHCenter);
menu->addWidget(logo);
menu->addLayout(buttons);
delete pixmap;
}
void MyMenu::construct_buttons()
{
buttons = new QHBoxLayout();
start = new QPushButton("Start", this);
buttons->addWidget(start);
setting = new QPushButton("Setting", this);
buttons->addWidget(setting);
tutorial = new QPushButton("Tutorial", this);
buttons->addWidget(tutorial);
exit = new QPushButton("Exit", this);
buttons->addWidget(exit);
}
void MyMenu::set_background()
{
QPalette *palette = new QPalette();
palette->setBrush(this->backgroundRole(),QBrush(QImage("/home/kahrabian/ClionProjects/Shooter-AP93UT/Contents/background_menu.jpg")));
this->setPalette(*palette);
delete palette;
}
My main.cpp code:
#include <QApplication>
#include "MyMenu.h"
#include "MyMainWindow.h"
int main(int argc, char **argv)
{
QApplication app (argc, argv);
MyMainWindow *mainwin = new MyMainWindow();
// MyMenu *MyMenu = new MyMenu();
// MyMenu->show();
return app.exec();
}
Can anyone help me with this problem??
Check this answer.
I would recommend you to use Qt style sheets.
You would need to call something like this:
setStyleSheet("image: url(path/to/background/image.png);");
on your widget.
Also, you might need to implement paintEvent() for widget to accept style sheets.
I'm usually doing it like this:
void MyWidget::paintEvent(QPaintEvent *pe)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
QWidget::paintEvent(pe);
}
How to add slot to toggle show and hide the toolbar in qmenu? This my code:
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setMinimumSize(800, 600);
CreateAct();
CreateMenus();
createToolBars();
}
void MainWindow::CreateAct()
{
undoAct = new QAction(QIcon::fromTheme("edit-undo", QIcon(":/images/undo.png")), tr("&Undo"), this);
redoAct = new QAction(QIcon::fromTheme("edit-redo", QIcon(":/images/redo.png")), tr("&Redo"), this);
cutAct = new QAction(QIcon::fromTheme("edit-cut", QIcon(":/images/cut.png")), tr("Cu&t"), this);
copyAct = new QAction(QIcon::fromTheme("edit-copy", QIcon(":/images/copy.png")), tr("&Copy"), this);
pasteAct = new QAction(QIcon::fromTheme("edit-paste", QIcon(":/images/paste.png")), tr("&Paste"), this);
editToolBarAct = new QAction(tr("Show edit toolbar"), this);
editToolBarAct->setCheckable(true);
editToolBarAct->setChecked(true);
// connect(editToolBarAct, SIGNAL(toggled(bool)), editToolBar, SLOT());
fileToolBarAct = new QAction(tr("Show file toolbar"), this);
fileToolBarAct->setCheckable(true);
fileToolBarAct->setChecked(true);
// connect(fileToolBarAct, SIGNAL(toggled(bool)), fileToolBar, SLOT());
}
void MainWindow::CreateMenus()
{
windowMenu = menuBar()->addMenu(tr("&Window"));
windowMenu->addAction(fileToolBarAct);
windowMenu->addAction(editToolBarAct);
}
void MainWindow::createToolBars()
{
fileToolBar = addToolBar("file");
fileToolBar->addAction(undoAct);
fileToolBar->addAction(redoAct);
fileToolBar->toggleViewAction();
fileToolBar->setAllowedAreas(Qt::TopToolBarArea | Qt::LeftToolBarArea);
editToolBar = addToolBar(tr("Edit"));
editToolBar->addAction(cutAct);
editToolBar->addAction(copyAct);
editToolBar->addAction(pasteAct);
editToolBar->setAllowedAreas(Qt::TopToolBarArea | Qt::LeftToolBarArea);
}
MainWindow::~MainWindow() {}
I am know using toggleViewAction, but how to use that code (I am really new in qt programming), can you give me sample code? I've tried googling, but did not find examples of its use.
The following small example demonstrates how to use the QToolBar::toggleViewAction():
class MainWindow : public QMainWindow
{
public:
MainWindow()
{
// Create a tool bar
QToolBar *tb = addToolBar("My Tool Bar");
[..]
// Create a menu and add toggle action for the tool bar.
QAction *tba = tb->toggleViewAction();
QMenu *m = menuBar()->addMenu("&Window");
m->addAction(tba);
}
};
I have a mainWindow and a Dialog in Qt . I am opening two images in MainWindow . After I make operations with image (crop, resize, rotate ) on MainWindow . I want to send images to another window (QDialog) . How can i send it as a parameter? My partial code is below ;
MainWindow::MainWindow()
{
openButton_1 = new QPushButton(tr("Open"));
cropButton_1 = new QPushButton(tr("Crop"));
rotateButton_1 = new QPushButton(tr("Rotate"));
resizeButton_1 = new QPushButton(tr("Resize"));
doneButton = new QPushButton(tr("Done"));
....
....
....
....
....
connect(openButton_1, SIGNAL(clicked()), this, SLOT(open1()));
connect(openButton_2, SIGNAL(clicked()), this, SLOT(open2()));
connect(doneButton, SIGNAL(clicked()), this, SLOT(done()));
// done() function for open new Window
void MainWindow::done()
{
CompareImage dialog(this);
dialog.exec();
}
// new dialog window
CompareImage::CompareImage( QWidget *parent ) : QDialog( parent )
{
pushButton = new QPushButton(tr("TesT"));
graphicsScene = new QGraphicsScene;
graphicsView = new QGraphicsView(graphicsScene);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget( pushButton );
mainLayout->addWidget( graphicsView );
setLayout( mainLayout );
}
// And here also my open() function
void MainWindow::open( int signal )
{
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open File"), QDir::currentPath());
if (!fileName.isEmpty()) {
QImage image(fileName);
if (image.isNull()) {
QMessageBox::information(this, tr("Image Viewer"),
tr("Cannot load %1.").arg(fileName));
return;
}
QGraphicsPixmapItem* item = new QGraphicsPixmapItem(QPixmap::fromImage(image));
if( signal == 1 )
{
graphicsScene_1->addItem(item);
graphicsView_1->show();
}
else if(signal == 2)
{
graphicsScene_2->addItem(item);
graphicsView_2->show();
}
}
}
It looks good idea to use QGraphicsPixmapItem* item but i couldnt make it .. Could you help me? Thanks for ideas..
> EDİT: here also my open1 and open2 function to understand situation clearly..
void MainWindow::open1()
{
open( 1 );
}
void MainWindow::open2()
{
open( 2 );
}
The good way to do it will be using signals/slots
1. In main window declaration add smth like:
signals:
void ImageProcessingDone(QImage& image);
2. In your dialog declare slot
public slosts:
void RecevedProcessedImage(QImage& image);
3. Implpement slot for processing image.
4. In constructo of main window connect signal and slot.
So when your image processing will be done just write in MainWindow emit ImageProcessingDone(imageInstance) and it will be transfered to your dialog
Say I have a QHBoxLayout where there are 2 QTextEdits and between them a button with an arrow to the right. When you click on the button, the right-side QTextEdit gradually closes by moving the left border until it meets the right one. Simultaneously, the right border of the left QTextEdit takes the place which the right QTextEdit released. And after pressing on the button, the state of the system is coming to the former one.
EDIT: In order to organize this I have done the following:
1) In header file:
class MyWidget : public QWidget
{
Q_OBJECT
QTextEdit *m_textEditor1;
QTextEdit *m_textEditor2;
QPushButton *m_pushButton;
QHBoxLayout *m_layout;
int m_deltaX;
public:
MyWidget(QWidget * parent = 0);
~MyWidget(){}
private slots:
void closeOrOpenTextEdit2(bool isClosing);
};
2) In the source file:
MyWidget::MyWidget(QWidget * parent):QWidget(parent),m_deltaX(0)
{
m_pushButton = new QPushButton(this);
m_pushButton->setText(">");
m_pushButton->setCheckable(true);
connect(m_pushButton, SIGNAL(clicked(bool)), this, SLOT(closeOrOpenTextEdit2(bool)));
m_textEditor1 = new QTextEdit(this);
m_textEditor1->setText("AAAAA AAAAAAAAAAA AAAAAAAAAAA AAAAAAA AAAAAAAAAAA AAAAAAAAAAA AA");
m_textEditor2 = new QTextEdit(this);
m_layout = new QHBoxLayout;
m_layout->addWidget(m_textEditor1);
m_layout->addWidget(m_pushButton);
m_layout->addWidget(m_textEditor2);
setLayout(m_layout);
}
void MyWidget::closeOrOpenTextEdit2(bool isClosing)
{
QPropertyAnimation *animation1 = new QPropertyAnimation(m_textEditor2, "geometry");
QPropertyAnimation *animation2 = new QPropertyAnimation(m_pushButton, "geometry");
QPropertyAnimation *animation3 = new QPropertyAnimation(m_textEditor1, "geometry");
if(isClosing) //close the second textEdit
{
m_pushButton->setText("<");
QRect te2_1 = m_textEditor2->geometry();
m_deltaX = te2_1.width()-3;
QRect te2_2(te2_1.x()+m_deltaX, te2_1.y(), 3 ,te2_1.height());
QRect pb_1 = m_pushButton->geometry();
QRect pb_2(pb_1.x()+m_deltaX, pb_1.y(), pb_1.width() ,pb_1.height());
QRect te1_1 = m_textEditor1->geometry();
QRect te1_2(te1_1.x(), te1_1.y(), te1_1.width()+m_deltaX, te1_1.height());
//animation->setDuration(10000);
animation1->setStartValue(te2_1);
animation1->setEndValue(te2_2);
animation2->setStartValue(pb_1);
animation2->setEndValue(pb_2);
animation3->setStartValue(te1_1);
animation3->setEndValue(te1_2);
}
else //open
{
m_pushButton->setText(">");
QRect te2_1 = m_textEditor2->geometry();
QRect te2_2(te2_1.x()-m_deltaX, te2_1.y(), 3+m_deltaX ,te2_1.height());
QRect pb_1 = m_pushButton->geometry();
QRect pb_2(pb_1.x()-m_deltaX, pb_1.y(), pb_1.width() ,pb_1.height());
QRect te1_1 = m_textEditor1->geometry();
QRect te1_2(te1_1.x(), te1_1.y(), te1_1.width()-m_deltaX, te1_1.height());
//animation->setDuration(10000);
animation1->setStartValue(te2_1);
animation1->setEndValue(te2_2);
animation2->setStartValue(pb_1);
animation2->setEndValue(pb_2);
animation3->setStartValue(te1_1);
animation3->setEndValue(te1_2);
}
animation1->start();
animation2->start();
animation3->start();
}
EDIT:
And I have the following problem:
When I close the second QTextEdit (by clicking on the button) and resize the MyWidget, then the QTextEdit restores its state (but it should stay closed of course). How can I solve this problem?
Please provide me with a code snippet.
Qt's Animation framework sounds like a good place to start. You could just try to follow their tutorials, adapting for you use case. I have used it already, and it seemed quite straight forward.
1) You could replace your button with a vertical layout, place the button inside this layout and finally add a vertical spacer below the button (in same the layout).
...
QVBoxLayout* m_buttonLayout = new QVBoxLayout();
m_layout = new QHBoxLayout();
m_layout->addWidget(m_textEditor1);
m_layout->addLayout(m_buttonLayout);
m_layout->addWidget(m_textEditor2);
m_buttonLayout->addWidget(m_pushButton);
m_buttonLayout->addItem( new QSpacerItem(1, 1, QSizePolicy::Minimum, QSizePolicy::Expanding) );
2) I guess you could (and should) animate widget's maximumSize (or just maximumWidth) property and let the layout take care of calculating actual geometries. This would also simplify your calculations. E.g.
QPropertyAnimation *animation1 = new QPropertyAnimation(m_textEditor2, "maximumWidth");
QPropertyAnimation *animation2 = new QPropertyAnimation(m_textEditor, "maximumWidth");
if (isClosing)
{
int textEdit2_start = m_textEditor2->maximumWidth();
int textEdit2_end = 0;
int textEdit_start = m_textEditor->maximumWidth();
int textEdit_end = textEdit_start + textEdit2_start;
animation1->setStartValue(textEdit2_start);
...
}
Also, now you don't have to animate buttons geometry at all (assuming that you have set fixed size to it).
PS. I didn't compile codes so there might be minor errors but you should get the idea.
Here what I wanted:
Header file
class MyWidget : public QWidget
{
Q_OBJECT
QTextEdit *m_textEditor1;
QTextEdit *m_textEditor2;
QPushButton *m_pushButton;
QHBoxLayout *m_layout;
QVBoxLayout *m_buttonLayout;
int m_deltaX;
bool m_isClosed;
public:
MyWidget(QWidget * parent = 0);
~MyWidget(){}
void resizeEvent( QResizeEvent * event );
private slots:
void closeOrOpenTextEdit2(bool isClosing);
};
Source file
MyWidget::MyWidget(QWidget * parent):QWidget(parent),m_deltaX(0)
{
m_pushButton = new QPushButton(this);
m_pushButton->setText(">");
m_pushButton->setCheckable(true);
m_pushButton->setFixedSize(16,16);
connect(m_pushButton, SIGNAL(clicked(bool)), this, SLOT(closeOrOpenTextEdit2(bool)));
m_textEditor1 = new QTextEdit(this);
m_textEditor1->setText("AAAAA AAAAAAAAAAA AAAAAAAAAAA AAAAAAA AAAAAAAAAAA AAAAAAAAAAA AA");
m_textEditor2 = new QTextEdit(this);
m_buttonLayout = new QVBoxLayout();
m_buttonLayout->addWidget(m_pushButton);
m_buttonLayout->addItem( new QSpacerItem(1, 1, QSizePolicy::Minimum, QSizePolicy::Expanding) );
m_layout = new QHBoxLayout;
m_layout->addWidget(m_textEditor1, 10);
m_layout->addSpacing(15);
m_layout->addLayout(m_buttonLayout);
m_layout->setSpacing(0);
m_layout->addWidget(m_textEditor2, 4);
setLayout(m_layout);
resize(800,500);
}
void MyWidget::closeOrOpenTextEdit2(bool isClosing)
{
m_isClosed = isClosing;
QPropertyAnimation *animation1 = new QPropertyAnimation(m_textEditor2, "maximumWidth");
if(isClosing) //close the second textEdit
{
m_textEditor2->setMaximumWidth(m_textEditor2->width());
int textEdit2_start = m_textEditor2->maximumWidth();
m_deltaX = textEdit2_start;
int textEdit2_end = 3;
animation1->setDuration(500);
animation1->setStartValue(textEdit2_start);
animation1->setEndValue(textEdit2_end);
m_pushButton->setText("<");
}
else //open
{
int textEdit2_start = m_textEditor2->maximumWidth();
int textEdit2_end = m_deltaX;
animation1->setDuration(500);
animation1->setStartValue(textEdit2_start);
animation1->setEndValue(textEdit2_end);
m_pushButton->setText(">");
}
animation1->start();
}
void MyWidget::resizeEvent( QResizeEvent * event )
{
if(!m_isClosed)
m_textEditor2->setMaximumWidth( QWIDGETSIZE_MAX );
}