I've 3 QFrame in the mainwindow, the frames looks like this:
I'd like to set a 60x20 QPushButton to the QFram I top right corner. I also resize the window and the frames resizes with the window.
I don't want to resize the push button, when the window resize.
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
window = new QWidget;
frame1 = new QFrame(window);
frame2 = new QFrame(window);
frame3 = new QFrame(window);
//check purpose
frame1->setFrameStyle(QFrame::Box);
frame2->setFrameStyle(QFrame::Box);
frame3->setFrameStyle(QFrame::Box);
button = new QPushButton(frame1);
button->resize(60,20);
setCentralWidget(window);
}
void MainWindow::resizeEvent(QResizeEvent *e) {
window->resize(e->size());
frame1->setGeometry(0, 0, e->size().width() * 0.5, e->size().height() * 0.4);
frame2->setGeometry(0, e->size().height() * 0.4, e->size().width() * 0.5, e->size().height() * 0.6);
frame3->setGeometry(e->size().width() * 0.5, 0, e->size().width() * 0.5, e->size().height());
}
Assuming button is the QPushButton you want to appear in the top right of frame you can probably use something like the following (untested)...
button = new QPushButton(frame1);
button->setFixedSize(60,20);
button->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
auto *layout = new QGridLayout(frame1);
layout->setContentsMargins(0, 0, 0, 0);
layout->setMargin(0);
layout->setSpacing(0);
layout->addWidget(button, 0, 1);
layout->setRowStretch(1, 1);
layout->setColumnStretch(0, 1);
Note the use of QWidget::setFixedSize rather than QWidget::resize.
Related
I have created a QLineEdit and few QPushButtons in the mainwindow ui and I set there size to (100,100).
If I resize the the window I would like to reize the buttons and a text box at runtime.
you should use layout
For Example look this :
QWidget *window = new QWidget;
QPushButton *button1 = new QPushButton("One");
QPushButton *button2 = new QPushButton("Two");
QPushButton *button3 = new QPushButton("Three");
QPushButton *button4 = new QPushButton("Four");
QPushButton *button5 = new QPushButton("Five");
QGridLayout *layout = new QGridLayout(window);
layout->addWidget(button1, 0, 0);
layout->addWidget(button2, 0, 1);
layout->addWidget(button3, 1, 0, 1, 2);
layout->addWidget(button4, 2, 0);
layout->addWidget(button5, 2, 1);
window->show();
I use QCheckBox in QTableWidgetCell
QWidget *widget = new QWidget();
QCheckBox *checkBox = new QCheckBox();
QHBoxLayout *layout = new QHBoxLayout(widget);
layout->addWidget(checkBox);
layout->setAlignment(Qt::AlignCenter);
layout->setContentsMargins(0, 0, 0, 0);
widget->setLayout(layout);
table->setCellWidget(0, 0, widget);
How can I change cell background?
The code:
widget->setStyleSheet("background-color: red");
works fine but you need to set the style for every container widget you add to your table:
So in order to see the change you need the following code:
QWidget *widget = new QWidget();
widget->setStyleSheet("background-color: red");
QCheckBox *checkBox = new QCheckBox();
QHBoxLayout *layout = new QHBoxLayout(widget);
layout->addWidget(checkBox);
layout->setAlignment(Qt::AlignCenter);
layout->setContentsMargins(0, 0, 0, 0);
widget->setLayout(layout);
QWidget *widget2 = new QWidget();
widget2->setStyleSheet("background-color: red");
QCheckBox *checkBox2 = new QCheckBox();
QHBoxLayout *layout2 = new QHBoxLayout(widget2);
layout2->addWidget(checkBox2);
layout2->setAlignment(Qt::AlignCenter);
layout2->setContentsMargins(0, 0, 0, 0);
widget2->setLayout(layout);
ui->tableWidget->setCellWidget(0, 0, widget);
ui->tableWidget->setCellWidget(0, 1, widget2);
And the result will be:
You should try this:
checkBox->setStyleSheet("background-color: red;");
If you want to specify it more generally, write the classtype in the CSS to indicate which class in the hierarchy should handle the flag. This could look something like this then:
QWidget { background-color: red; }
If you want to change cell background, not a widget, use setBackground() method:
QCheckBox *checkBox = new QCheckBox("example");
QWidget *widget = new QWidget();
QHBoxLayout *layout = new QHBoxLayout(widget);
layout->addWidget(checkBox);
layout->setAlignment(Qt::AlignCenter);
layout->setContentsMargins(0, 0, 0, 0);
widget->setLayout(layout);
ui->tableWidget_2->setCellWidget(0,0,widget);
ui->tableWidget_2->item(0, 0)->setBackground(Qt::red);//this line should be
In this case all your cell will be red (without white lines around checkbox).
I've subclassed QWidget and defined constructor this way:
LoupingWidget::LoupingWidget(QWidget *parent): QWidget(parent)
{
QGroupBox *topGroupBox = new QGroupBox(this);
QGraphicsView *xRGBPlot = new QGraphicsView(this);
QGraphicsView *yRGBPlot = new QGraphicsView(this);
QGraphicsView *loupe = new QGraphicsView(this);
QSlider *slider = new QSlider(this);
QGridLayout *boxGLayout = new QGridLayout;
boxGLayout->addWidget(xRGBPlot, 0, 0);
boxGLayout->addWidget(slider, 0, 1);
boxGLayout->addWidget(loupe, 1, 0);
boxGLayout->addWidget(yRGBPlot, 1, 1);
topGroupBox->setLayout(boxGLayout);
}
Next, I am trying to add it in a QDialog:
Window::Window(QWidget *parent): QDialog(parent)
{
LoupingWidget *firstLoupindWidget = new LoupingWidget(this);
LoupingWidget *secondLoupindWidget = new LoupingWidget(this);
// QGraphicsView *mainPicture = new QGraphicsView(this);
QGridLayout *gridLayout = new QGridLayout;
// gridLayout->addWidget(mainPicture, 0, 0);
gridLayout->addWidget(firstLoupindWidget, 1, 0);
gridLayout->addWidget(secondLoupindWidget, 1, 1);
setLayout(gridLayout);
}
When this two lines are commented out, two widgets are placed horizontally.
And that's good, but when I uncomment lines with another QGraphicsViews, it fills entire window.
What am I doing wrong?
LoupingWidget doesn't have a layout, so when it's added to another layout, layout can't resize it according to its contents. You need to create another layout (e.g. QGridLayout) in LoupingWidget constructor, add topGroupBox to the layout and set the layout as LoupingWidget's layout.
I'm trying to test animations in Qt desktop application. I just copied example from help. After button click, new button just appear in left top corner without animation (even end position is wrong). Am I missing something?
Qt 5.0.1, Linux Mint 64bit, GTK
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPropertyAnimation>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QPushButton *button = new QPushButton("Animated Button", this);
button->show();
QPropertyAnimation animation(button, "geometry");
animation.setDuration(10000);
animation.setStartValue(QRect(0, 0, 100, 30));
animation.setEndValue(QRect(250, 250, 100, 30));
animation.start();
}
Edit: Solved. Animation object must be as global reference. For example in section private QPropertyAnimation *animation. Then QPropertyAnimation = New(....);
You don't need to make a slot specifically for deleting the mAnimation variable. Qt can do it for you if you use QAbstractAnimation::DeleteWhenStopped:
QPropertyAnimation *mAnimation = new QPropertyAnimation(button, "geometry");
mAnimation->setDuration(10000);
mAnimation->setStartValue(QRect(0, 0, 100, 30));
mAnimation->setEndValue(QRect(250, 250, 100, 30));
mAnimation->start(QAbstractAnimation::DeleteWhenStopped);
You just didn't copied the example, you also made some changes that broke it. Your animation variable is now a local variable that is destroyed at the end of on_pushButton_clicked function. Make the QPropertyAnimation instance a member variable of the MainWindow class and use it like this:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow), mAnimation(0)
{
ui->setupUi(this);
QPropertyAnimation animation
}
MainWindow::~MainWindow()
{
delete mAnimation;
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QPushButton *button = new QPushButton("Animated Button", this);
button->show();
mAnimation = new QPropertyAnimation(button, "geometry");
mAnimation->setDuration(10000);
mAnimation->setStartValue(QRect(0, 0, 100, 30));
mAnimation->setEndValue(QRect(250, 250, 100, 30));
mAnimation->start();
}
I am attaching some parts of my code,
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "colorrecognition.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//this->setStyleSheet("background-color: black;"); // set the background
//capture = 0;
// frame = 0;
//interface
//tabWidget = new QTabWidget;
//start capturing a video
//capture = cvCaptureFromCAM(0);
//capture = cvCaptureFromAVI("videoExample.avi");
//frame = cvQueryFrame(capture);
tabWidget = new QTabWidget(ui->centralWidget);
tabWidget->setObjectName(QString::fromUtf8("tabWidget"));
tabWidget->setGeometry(QRect(20, 0, 801, 571));
tabWidget->addTab(new ColorRecognition(), tr("Color Recognition"));
Contour contour1;
}
MainWindow::~MainWindow()
{
delete ui;
}
//========================================================
ColorRecognition::ColorRecognition(QWidget *parent){
storage1 = cvCreateMemStorage(0);
storage2 = cvCreateMemStorage(0);
/*QFrame* frame1 = new QFrame(this);
frame1->setObjectName(QString::fromUtf8("frame1"));
frame1->setGeometry(QRect(20, 20, 761, 501));
frame1->setFrameShape(QFrame::StyledPanel);
frame1->setFrameShadow(QFrame::Raised);*/
//start capturing a video
capture = cvCaptureFromCAM(0);
//capture = cvCaptureFromAVI("videoExample.avi");
//print an error message in case you can't grab the frame
if (!cvGrabFrame(capture)) { // capture a frame
printf("couldn't grab a frame");
}
//First screen --- Color Identification ---
//QObject::connect(ui->tabWidget, SIGNAL(currentChanged(int)), this, SLOT(color_Recoginition()));
//set timer for 50ms intervals
QTimer* m_timer = new QTimer(this);
connect(m_timer, SIGNAL(timeout()), this, SLOT(color()));
m_timer->start(100);
}
void ColorRecognition::paintEvent(QPaintEvent* e) {
QPainter painter(this);
painter.drawImage(10,10, qt_img);
}
void ColorRecognition::color(){
frame = cvQueryFrame(capture);
//CvSize imageSize = cvSize(frame->width,frame->height);
//IplImage* resultImage = cvCreateImage(imageSize, 8, 3);
//qDebug("Befroe manipulation");
cvCvtColor(frame, frame, CV_BGR2RGB);
//CvSize imageSize = cvSize(frame->width, frame->height);
IplImage **thresholdedImage = contour1.GetThresholdedImage(frame);
//insert the resulted frame from the above function into the find contour function
cvFindContours(thresholdedImage[0], storage1, &contours1, sizeof(CvContour),
CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));
cvFindContours(thresholdedImage[1], storage2, &contours2, sizeof(CvContour),
CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));
contour1.drawContour(contours1, storage1, frame, CV_RGB(255, 0, 0));
contour1.drawContour(contours2, storage2, frame, CV_RGB(0, 255, 0));
qt_img = QImage((unsigned char *)frame->imageDataOrigin,frame->width,frame->height,QImage::Format_RGB888);
//release temp memory
//cvReleaseMemStorage(&storage1);
//cvReleaseMemStorage(&storage2);
//cvReleaseMemStorage(&contours1->storage); // check if that is correct
//cvReleaseMemStorage(&contours2->storage);*/
//resultImage = contour1.colorIdentification(frame);
//cvCvtColor(resultImage, resultImage, CV_BGR2RGB);
//qDebug("After manipulation");
this->update();
}
the problem is that when I run this code I don't get anything on the screen, however when I run the code out Qt (only in OpenCV) the code was working with no problems
Can anyone know what is the deal ?
so I figured out what went wrong in my code, it is really a silly mistake -->
I was passing the frame image immediately to the function I have done
frame = cvQueryFrame(capture);
so what solved my problem was copying the frame image into an IplImage and then it worked perfectly -->
cvCopy(frame, resultImage);