tags:

views:

93

answers:

1

I amk new to qt. I tried to create a window with a widget. The widget contains a picture(a chess board). Now, If I tried to show the window it is showing a part of that picture. Here the code

#include<QApplication>
#include<QMainWindow>
#include<QWidget>
#include<QMenu>
#include<QMenuBar>
#include<QPainter>
#include<QFrame>
#include<QHBoxLayout>
#include<iostream>

using namespace std;

class MyWindow:public QMainWindow
{
public:
MyWindow();
};

class MyWidget:public QWidget
{
public:
MyWidget();
void paintEvent(QPaintEvent * event);
};

int main(int argc,char *argv[])
{
Q_INIT_RESOURCE(puzzle);

QApplication app(argc,argv);

MyWindow mainWindow;

mainWindow.show();

return app.exec();
}

MyWindow::MyWindow():QMainWindow()
{
setSizePolicy(QSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed));

QMenu * fileMenu=menuBar()->addMenu(QObject::tr("Options"));

QAction * restartAction = fileMenu->addAction(tr("NewGame"));

QAction * exitAction = fileMenu->addAction(tr("Exit"));
exitAction->setShortcuts(QKeySequence::Quit);

QWidget * tempWidget=new MyWidget();

QFrame * newFrame=new QFrame();
QHBoxLayout * horizontal= new QHBoxLayout(newFrame);

horizontal->addWidget(tempWidget);
setCentralWidget(newFrame);
}

MyWidget::MyWidget():QWidget()
{
setMinimumSize(100,100);
setMaximumSize(1000,1000);
}

void MyWidget::paintEvent(QPaintEvent * event)
{
QPainter painter;
painter.begin(this);
painter.drawPixmap(QRect(0,0,500,600),QPixmap("Board").scaled(QSize(500,600),Qt::KeepAspectRatioByExpanding,Qt::SmoothTransformation));
painter.end();
}
A: 

Hello,

In you paintEvent you are forcing the image to be painted to 500x600 pixels but you are not giving a fix size to the widget. So if the widget is smaller than that you cannot see the full image. If you do a setFixedSize(500, 600) it should fix your problem.

You are also doing

setSizePolicy(QSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed));

in the main window. Are you giving a fix size apart from setting the size policy? If size policy is fixed you should specify a size for the window.

Also I think you can call setSizePolicy(QSizePolicy::Fixed); directly.

cnebrera
Ya, it's working by changing the size of the window using setFixedSize. Is it possible to make the window which can adjust it's size according to the widget. And can you say how to make a widget which can adjust it's size according to the image it got.
prabhakaran
But still I want to get a scrollbar
prabhakaran
cnebrera
What do you mean by the scrollbar? What do you want to do exactly?
cnebrera
see in the above "code window" there is one vertical scroll bar, and one horizontal scroll bar. Like that I want to have those in my window or widget.
prabhakaran
Why dont you use a QScrollArea as content for your MainWindow and use the QWidget with the fixed size of the picture as content for the QScrollArea. This should give you the scrolls you need.
cnebrera
@cnebrera thank you, it works. I think I will meet you again in some qt question
prabhakaran
I'm glad to help :)
cnebrera