Hello,
i have center a QWidget in a QScrollArea, but when i resize the QWidget move that to the left corner on top.
Header file:
#ifndef TESTWIDGET_H
#define TESTWIDGET_H
#include <QtGui>
Q_OBJECT
public:
TestWidget
(QWidget * parent
=0,
const char * name
=0, Qt
::WFlags f
=0);
~TestWidget();
public slots:
void slotShrink();
};
#endif
#ifndef TESTWIDGET_H
#define TESTWIDGET_H
#include <QtGui>
class TestWidget : public QWidget{
Q_OBJECT
public:
TestWidget(QWidget * parent=0, const char * name=0, Qt::WFlags f=0);
~TestWidget();
QPushButton *bZoom;
public slots:
void slotShrink();
};
#endif
To copy to clipboard, switch view to plain text mode
Implementation:
#include "myWidget.h"
TestWidget
::TestWidget(QWidget * parent,
const char * name, Qt
::WFlags f
):QWidget(parent, f
){
bZoom->setText("Shrink");
connect(bZoom, SIGNAL(clicked()), this, SLOT(slotShrink()));
}
void TestWidget::slotShrink()
{
this->resize(this->width()-5, this->height()-5);
}
TestWidget::~TestWidget()
{
}
#include "myWidget.h"
TestWidget::TestWidget(QWidget * parent, const char * name, Qt::WFlags f):QWidget(parent, f)
{
bZoom = new QPushButton(this);
bZoom->setText("Shrink");
connect(bZoom, SIGNAL(clicked()), this, SLOT(slotShrink()));
}
void TestWidget::slotShrink()
{
this->resize(this->width()-5, this->height()-5);
}
TestWidget::~TestWidget()
{
}
To copy to clipboard, switch view to plain text mode
Main:
#include <QApplication>
#include <QMainWindow>
#include <QScrollArea>
#include "myWidget.h"
int main(int argc, char *argv[])
{
mw->resize(400,400);
sa->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
QWidget *qw
= new TestWidget
( sa
);
QObject::connect(qw,
SIGNAL(clicked
()),
&a,
SLOT(slotShrink
()));
qw->resize(200,200);
sa->setWidget(qw);
mw->setCentralWidget( sa );
mw->show();
return a.exec();
}
#include <QApplication>
#include <QMainWindow>
#include <QScrollArea>
#include "myWidget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow *mw = new QMainWindow();
mw->resize(400,400);
QScrollArea *sa = new QScrollArea( mw );
sa->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
sa->setBackgroundRole(QPalette::Dark);
QWidget *qw = new TestWidget( sa );
QObject::connect(qw, SIGNAL(clicked()), &a, SLOT(slotShrink()));
qw->setPalette( QPalette( QColor(0,0,255) ) );
qw->resize(200,200);
sa->setWidget(qw);
mw->setCentralWidget( sa );
mw->show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
What is the problem, can anyone help me?
Thanks.
Bookmarks