PlayScene::PlayScene(){ //creat a scene QGraphicsScene *scene = new QGraphicsScene(); scene->setSceneRect(0,0,1024,768); scene->setBackgroundBrush(QBrush(QImage("D:/img/img/bg.png"))); //add a view QGraphicsView *view = new QGraphicsView(scene); view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); view->setFixedSize(1024,768); //add Rec Rec *rec = new Rec(); rec->setFlag(QGraphicsItem::ItemIsFocusable); rec->setFocus(); scene->addItem(rec); BlueHit *bluehit = new BlueHit(); scene->addItem(bluehit); bluehit->setPos(80,190); //QTimer *timer = new QTimer(); //QObject::connect(timer,SIGNAL(timeout()),bluehit,SLOT(create())); Drum *drum = new Drum(); scene->addItem(drum); drum->setPos(1000,250); //show the view view->show(); }
Game::Game() { srand(time(0)); //Create main game scene QGraphicsScene *game_screen = new QGraphicsScene(this); //Create player tank Player *player = new Player('U'); player->setRect(0, 0, 31, 31); game_screen->addItem(player); player->setFlag(QGraphicsItem::ItemIsFocusable); player->setFocus(); QGraphicsView *screen = new QGraphicsView(game_screen); screen->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); screen->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); screen->show(); screen->setFixedSize(800, 600); game_screen->setSceneRect(0, 0, 800, 600); //player->setPos(screen->width()/2+1, screen->height() - player->rect().height()-5); player->setPos(32*4,32*12); //player->setPos(screen->width()/2+1, screen->height() - player->rect().height()-4); Generation(game_screen); }
/** Add a square to the grid * @brief MainWindow::addSquare * @param definition * @param i * @param j * @param arrowIndex */ void MainWindowController::addSquare(int definition, int x, int y) { int index = (y*view->getGridW())+x; view->getSquareContainer()[index].setSizeConstraint(QLayout::SetFixedSize); int defNum = -1; if(definition != 0) defNum = this->counter; SquareScene* scene = new SquareScene(defNum, x, y); QGraphicsView* gview = new QGraphicsView(scene); gview->setFixedSize(50,50); gview->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); gview->setFocusPolicy(Qt::ClickFocus); gview->setAlignment(Qt::AlignLeft | Qt::AlignTop); if(definition >= 1) { addDefinitionSquare(gview, scene); } else { //Make connections for standardSquare connect(scene, SIGNAL(clicked(SquareScene*)), this, SLOT(slotClickStdSquare(SquareScene*))); connect(scene, SIGNAL(letterkeypress(SquareScene*,QString)), this, SLOT(slotLetterKeyPress(SquareScene*,QString)) ); connect(scene, SIGNAL(delkeypress(SquareScene*)), this, SLOT(slotDelKeyPress(SquareScene*)) ); connect(scene, SIGNAL(leftClicked(SquareScene*,QPoint)), this, SLOT(slotLeftClickStd(SquareScene*,QPoint))); } view->getSquareContainer()[index].addWidget(gview); if(definition == 2) { SquareScene* scene2 = new SquareScene(this->counter,x ,y, true); QGraphicsView* gview2 = new QGraphicsView(scene2); gview->setFixedSize(50,25); gview2->setFixedSize(50,25); gview2->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); gview2->setAlignment(Qt::AlignLeft | Qt::AlignTop); gview2->setFocusPolicy(Qt::ClickFocus); addDefinitionSquare(gview2, scene2); view->getSquareContainer()[index].addWidget(gview2); ( *(view->getSquareView()) ).push_back(gview2); ( *(view->getSquareScene()) ).push_back(scene2); } view->getSquareView()->push_back(gview); view->getSquareScene()->push_back(scene); view->getUi()->gameLayout->addLayout(&(view->getSquareContainer())[index], y, x); }
void FenPrincipale::LoadImageResult(char* str) { scene = new MyScene(QString(str)); QGraphicsView *view = new QGraphicsView(scene); view->setFixedSize(scene->size+QSize(10,10)); //view->show(); //image->setPixmap(QPixmap(fileName)); QVBoxLayout *dockLayout = new QVBoxLayout; dockLayout->addWidget(view); ImageWidget->setLayout(dockLayout); dockImage->show(); }
void MainWindow::on_search_clicked() { //validate if(imageToSearch.isEmpty()) { ui->message->setText("Debe cargar una imagen para la búsqueda"); return; } QSettings settings(SETTINGS_FNAME,QSettings::IniFormat); QString folder = settings.value("rootFolder",QVariant(".")).toString(); ui->message->setText("detectando coincidencias"); classifier.detect(imread(imageToSearch.toStdString().c_str())); QVector<QString> matches = classifier.getBestMatches(); //mostrar resultados //limpiar QLayoutItem *wItem; while ((wItem = ui->resultsLayout->takeAt(0)) != 0) { ui->resultsLayout->removeItem(wItem); delete wItem; } //mostrar foreach(QString match, matches) { //show results QLabel* label = new QLabel; label->setText(match.toUpper()); ui->resultsLayout->addWidget(label); QGraphicsView* graphics = new QGraphicsView; QGraphicsScene *scn = new QGraphicsScene( graphics ); scn->setSceneRect(QRect(1,1,198,98)); graphics->setScene( scn ); graphics->setFixedSize(200, 100 ); QPixmap pix( folder+QDir::separator()+match); scn->addPixmap( pix ); ui->resultsLayout->addWidget(graphics); QSpacerItem* space = new QSpacerItem(100,20); ui->resultsLayout->addSpacerItem(space); }
/*Tutorial: QGraphicsScene: world, player (Inivisble ~concept) QGraphicsItem(QGraphicsRectitem): goes into scene QGraphicsView: */ int main(int argc, char *argv[]) { QApplication a(argc, argv); //Create a scene QGraphicsScene *scene = new QGraphicsScene(); //Create an item to put into the scene MyRect *rect = new MyRect(); rect->setRect(0,0,100,100); //(x,y,width,height) //Add item to the Scene scene->addItem(rect); //Make Rect focusable rect->setFlag(QGraphicsItem::ItemIsFocusable); //Only one focused item at a time //Allowing rect to be focusable rect->setFocus(); //Setting rect item to focus //Add a view QGraphicsView *view = new QGraphicsView(scene); //Initializing in constructor //view->setScene(scene); alternative view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); view->show(); view->setFixedSize(800,600); scene->setSceneRect(0,0,800,600); rect->setPos(view->width()/2,view->height() - rect->rect().height()); //Spawn Enemies QTimer *timer = new QTimer(); QObject::connect(timer,SIGNAL(timeout()),rect,SLOT(spawn())); timer->start(2000); return a.exec(); }
int main(int argc, char *argv[]) { QApplication a(argc, argv); // Create a scene QGraphicsScene * mygamescene = new QGraphicsScene(); MyRect * player = new MyRect(); player->setRect(0,0,50,50); mygamescene->addItem(player); // Make rect respond to events player->setFlag(QGraphicsItem::ItemIsFocusable); player->setFocus(); QGraphicsView * myview = new QGraphicsView(mygamescene); myview->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); myview->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); myview->show(); myview->setFixedSize(800,600); mygamescene->setSceneRect(0,0,800,600); player->setPos((myview->width()-player->rect().width())/2,myview->height()-player->rect().height()); return a.exec(); }
int main(int argc, char *argv[]) { QApplication a(argc, argv); // create scene QGraphicsScene *scene = new QGraphicsScene(); // create Item MyRec * player = new MyRec(); //add Item scene->addItem(player); //make focusable player->setFlag(QGraphicsItem::ItemIsFocusable); player->setFocus(); //spawn enemy QTimer *timer = new QTimer; QObject::connect(timer,SIGNAL(timeout()),player,SLOT(spawn())); timer->start(2000); // vitualize QGraphicsView *view = new QGraphicsView(scene); view->setSceneRect(0,0,800,600); view->setFixedSize(800,600); //set initial position player player->setPos(view->width()/2,view->height()-player->rect().height()); view->show(); // view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); // view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); return a.exec(); }
/* GameBored contstructor * @param QWidget is the parent widget */ GameBored::GameBored(QWidget *parent) : QWidget(parent), ui(new Ui::GameBored) { ui->setupUi(this); // make scene QGraphicsScene * scene= new QGraphicsScene(); scene->setSceneRect(0,0,800,600); scene->setBackgroundBrush(QBrush(QImage(":/pics/bg"))); // make and add item to scene MyRect * player = new MyRect(); scene->addItem(player); // focus item so that it can receive keyboard events player->setFlag(QGraphicsItem::ItemIsFocusable); player->setFocus(); // add scene to new view QGraphicsView * view = new QGraphicsView(scene); //turn off scrollbars view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); Board= new QWidget; //Board->setFixedSize(400,400); // add view to new layout // add layout to central widget QVBoxLayout * vlayout = new QVBoxLayout(Board); vlayout->addWidget(view); this->setLayout(vlayout); // this->show(); view->setFixedSize(800,600); scene->setSceneRect(0,0,800,600); player->setPos(view->width()/2 -player->pixmap().width()/2, -28 /*view->height()- player->pixmap().height()*/); // constant creation of enemies QTimer* timer = new QTimer(); QObject::connect(timer,SIGNAL(timeout()),player,SLOT(spawn())); timer->start(2400); QTimer* timer2 = new QTimer(); QObject::connect(timer2,SIGNAL(timeout()),player,SLOT(spawn2())); timer2->start(5000); // play music mp3player = new QMediaPlayer(); mp3player->setMedia(QUrl("qrc:/audio/starfox.mp3")); mp3player->play(); // add score class to screen score = new Score(); scene->addItem(score); }
CardEditor::CardEditor(QWidget *parent) : QMainWindow(parent) { setWindowTitle(tr("Card editor")); QHBoxLayout *layout = new QHBoxLayout; QGraphicsView *view = new QGraphicsView; view->setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform | QPainter::HighQualityAntialiasing); card_scene = new CardScene; connect(card_scene, SIGNAL(avatar_snapped(QRectF)), this, SLOT(saveAvatar(QRectF))); view->setScene(card_scene); view->setFixedSize(card_scene->sceneRect().width() + 2, card_scene->sceneRect().height() + 2); layout->addWidget(createLeft()); layout->addWidget(view); QWidget *central_widget = new QWidget; central_widget->setLayout(layout); setCentralWidget(central_widget); QMenuBar *menu_bar = new QMenuBar; setMenuBar(menu_bar); QMenu *file_menu = new QMenu(tr("File")); QAction *import = new QAction(tr("Import ..."), file_menu); import->setShortcut(Qt::CTRL + Qt::Key_O); QAction *save = new QAction(tr("Save ..."), file_menu); save->setShortcut(Qt::CTRL + Qt::Key_S); QAction *exit = new QAction(tr("Exit"), file_menu); exit->setShortcut(Qt::CTRL + Qt::Key_Q); file_menu->addAction(import); file_menu->addAction(save); file_menu->addSeparator(); file_menu->addAction(exit); menu_bar->addMenu(file_menu); connect(import, SIGNAL(triggered()), this, SLOT(import())); connect(save, SIGNAL(triggered()), this, SLOT(saveImage())); connect(exit, SIGNAL(triggered()), this, SLOT(close())); QMenu *tool_menu = new QMenu(tr("Tool")); QAction *making_big = new QAction(tr("Make big avatar"), tool_menu); making_big->setShortcut(Qt::ALT + Qt::Key_B); connect(making_big, SIGNAL(triggered()), card_scene, SLOT(makeBigAvatar())); tool_menu->addAction(making_big); QAction *making_small = new QAction(tr("Make small avatar"), tool_menu); making_small->setShortcut(Qt::ALT + Qt::Key_M); connect(making_small, SIGNAL(triggered()), card_scene, SLOT(makeSmallAvatar())); tool_menu->addAction(making_small); QAction *making_tiny = new QAction(tr("Make tiny avatar"), tool_menu); making_tiny->setShortcut(Qt::ALT + Qt::Key_T); connect(making_tiny, SIGNAL(triggered()), card_scene, SLOT(makeTinyAvatar())); tool_menu->addAction(making_tiny); QAction *hiding_rect = new QAction(tr("Hide avatar rect"), tool_menu); hiding_rect->setShortcut(Qt::ALT + Qt::Key_H); connect(hiding_rect, SIGNAL(triggered()), card_scene, SLOT(hideAvatarRects())); tool_menu->addAction(hiding_rect); tool_menu->addSeparator(); QAction *reset_photo = new QAction(tr("Reset photo"), tool_menu); reset_photo->setShortcut(Qt::ALT + Qt::Key_R); connect(reset_photo, SIGNAL(triggered()), card_scene, SLOT(resetPhoto())); tool_menu->addAction(reset_photo); QAction *copy_photo = new QAction(tr("Copy photo to clipboard"), tool_menu); copy_photo->setShortcut(Qt::CTRL + Qt::Key_C); connect(copy_photo, SIGNAL(triggered()), this, SLOT(copyPhoto())); tool_menu->addAction(copy_photo); menu_bar->addMenu(tool_menu); card_scene->setMenu(tool_menu); }
//create all sub-widgets and display everything void PlayScreen::display(int screenWidth, int screenHeight, int gridSize) { this->screenWidth = screenWidth; this->screenHeight = screenHeight; grid = gridSize; numMoves = 0; seconds = 0; percentComplete = grid*grid; QFont font("Helvectica", 13); //set up scene and view QGraphicsScene *gScene = new QGraphicsScene(this); QGraphicsView *gView = new QGraphicsView(gScene); gView->setFixedSize(screenWidth, screenHeight); gScene->setBackgroundBrush(Qt::black); //set up all GridLayouts QGridLayout *layout = new QGridLayout(gView); playGrid = new QGridLayout(); QGridLayout *menuGrid = new QGridLayout(); layout->setContentsMargins(0,0,0,0); playGrid->setContentsMargins(0,0,0,0); menuGrid->setContentsMargins(0,0,0,0); gView->setLayout(layout); layout->addLayout(playGrid, 0, 0); layout->addLayout(menuGrid, 1, 0); //timer and move labels movesLabel = new QLabel("Moves: " + QString::number(numMoves)); timerLabel = new QLabel("Time: 0:0"); percentLabel = new QLabel("Percent: 0%"); menuGrid->addWidget(movesLabel,0,0); menuGrid->addWidget(timerLabel,1,0); menuGrid->addWidget(percentLabel,2,0); movesLabel->setFont(font); timerLabel->setFont(font); percentLabel->setFont(font); //menu buttons QPushButton *winButton = new QPushButton("DEBUG WIN"); QPushButton *pauseButton = new QPushButton("Pause/Play"); QPushButton *giveUpButton = new QPushButton("Give Up"); menuGrid->addWidget(winButton, 0, 1); menuGrid->addWidget(pauseButton, 1, 1); menuGrid->addWidget(giveUpButton, 2, 1); winButton->setFont(font); pauseButton->setFont(font); giveUpButton->setFont(font); connect(winButton, SIGNAL(clicked()), this, SLOT(winButtonClicked())); connect(pauseButton, SIGNAL(clicked()), this, SLOT(pauseButtonClicked())); connect(giveUpButton, SIGNAL(clicked()), this, SLOT(giveUpButtonClicked())); //import image QImageReader reader(imgPath); reader.setScaledSize(QSize(screenWidth, screenWidth)); QImage image = reader.read(); int eHeight = image.height(); int eWidth = image.width(); //cut image into tiles and position them for(int i = 0; i < grid; i++){ for(int j = 0; j < grid; j++){ if(!(i==grid-1 && j==grid-1)){ QPixmap pixmap = QPixmap::fromImage(image.copy(i*(eWidth/grid), j*(eHeight/grid), eWidth/grid, eHeight/grid)); QIcon icon(pixmap); Tile *button = new Tile(i, j, icon); button->setIconSize(QSize(eWidth/grid, eHeight/grid)); playGrid->addWidget(button, j, i); connect(button, SIGNAL(tileClicked(Tile*)), this, SLOT(handleTileClick(Tile*))); } } }
FenPrincipale::FenPrincipale() { // setFixedSize(1200,800); // Dimensions fixées à 850x480px QGridLayout *layout0 = new QGridLayout; // c le layout de base de la page1 QGraphicsScene *scene = new QGraphicsScene; QGraphicsView *vue = new QGraphicsView(scene,this); scene->setSceneRect(0,0,mapLargeur+50,mapHauteur+50); // taille de la scene vue->setRenderHints(QPainter::Antialiasing|QPainter::TextAntialiasing); vue->setFixedSize(mapLargeur+100,mapHauteur+100); //taille de la vue //Mise en place de la map QPixmap map_im ; map_im.load("map.png"); map=scene->addPixmap(map_im); map->setPos(0,0); map->setZValue(-1); //Rectangle test_debug QRect rectangle1(15,15,618,418); scene->addRect(rectangle1); //rectangle1.moveCenter(QPoint(0,0)); // met le centre au centre //Création de l'origine QRect rectangle(0,0,10,10); rectangle.moveCenter(QPoint(0,0)); // met le centre au centre origine=scene->addRect(rectangle); origine->setPos(map_im.width()/2,map_im.height()/2);// au centre de l'image //Mise en place de du robot QPixmap robot_im ; robot_im.load("petit_robot.png"); robot=scene->addPixmap(robot_im); robot->setZValue(2);// au dessus de l'image carte 2>-1 robot->setOffset(-robot_im.width()/2,-robot_im.height()/2); // origine du robot au centre robot->setPos(origine->x(),origine->y());//placer au centre du palteau //Mise en place de l'obstacle QPen pen(Qt::black, 1, Qt::SolidLine); QBrush brush(Qt::black); //scene.addRect(rectangle, pen, brush); obstacle = scene->addEllipse(0,0,15,15,pen,brush); obstacle->setZValue(1); obstacle->setPos(origine->x(),origine->y()); obstacle2 = scene->addEllipse(0,0,15,15,pen,brush); obstacle2->setZValue(1); obstacle2->setPos(origine->x(),origine->y()); //Ajout au layout principale puis à la fenetre layout0->addWidget(vue, 0,0); setLayout(layout0); //Configuration du Timer pas utiliser (debug) timer = new QTimer(this); // timer->start(1000); //Connection des Signaux/Solts connect( timer, SIGNAL(timeout()), this, SLOT(timerOut()) ); //Pacement par defaut //modifPosition(robot,-750,-1250,90); modifPosition(robot,-750,0,0); testPosObs(obstacle,100,90); }