Guest User

Untitled

a guest
Nov 19th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. void MainWindow::slotConstructWaterBodies(const QString& mapName, const QString& mapImagePath)
  2. {
  3. ui->mylabel->setPixmap(mapImagePath);
  4. /*Прочий код*/
  5. }
  6.  
  7. void MainWindow::resizeEvent(QResizeEvent *event)
  8. {
  9. adjustWidgetSize();
  10.  
  11. QMainWindow::resizeEvent(event);
  12. }
  13.  
  14. // Задает фиксированный размер виджета по ширине для сохранения пропорций картинки внутри него
  15. void MainWindow::adjustWidgetSize()
  16. {
  17. int widthCurrentPixMap = 1;
  18. int heightCurrentPixMap = 1;
  19.  
  20. if(pixPtr_) {
  21. widthCurrentPixMap = pixPtr_->width();
  22. heightCurrentPixMap = pixPtr_->height();
  23. }
  24. const QSize pixmapSize(widthCurrentPixMap, heightCurrentPixMap);
  25.  
  26. ui->mylabel->setFixedWidth(
  27. ui->centralWidget->height() * pixmapSize.width() / pixmapSize.height()
  28. );
  29. }
  30.  
  31. void MainWindow::slotConstructWaterBodies(const QString& mapName, const QString& mapImagePath)
  32. {
  33. QPixmap *pixmap = new QPixmap(mapImagePath);
  34. pixPtr_ = pixmap;
  35.  
  36. QGraphicsScene *scene = new QGraphicsScene;
  37. QGraphicsPixmapItem *item = new QGraphicsPixmapItem(*pixPtr_);
  38. item->setTransformationMode(Qt::SmoothTransformation);
  39. scene->addItem(item);
  40. ui->graphicsViewMap->setScene(scene);
  41. ui->graphicsViewMap->fitInView(scene->sceneRect(), Qt::IgnoreAspectRatio);
  42.  
  43. /*Прочий код*/
  44. }
  45.  
  46. void MainWindow::resizeEvent(QResizeEvent *event)
  47. {
  48. adjustWidgetSize();
  49.  
  50. adjustScaledPixmap();
  51.  
  52. QMainWindow::resizeEvent(event);
  53. }
  54.  
  55. // Задает фиксированный размер виджета по ширине для сохранения пропорций картинки внутри него
  56. void MainWindow::adjustWidgetSize()
  57. {
  58. int widthCurrentPixMap = 1;
  59. int heightCurrentPixMap = 1;
  60.  
  61. if(pixPtr_) {
  62. widthCurrentPixMap = pixPtr_->width();
  63. heightCurrentPixMap = pixPtr_->height();
  64. }
  65. const QSize pixmapSize(widthCurrentPixMap, heightCurrentPixMap);
  66.  
  67. ui->graphicsViewMap->setFixedWidth(
  68. ui->centralWidget->height() * pixmapSize.width() / pixmapSize.height()
  69. );
  70. }
  71.  
  72. void MainWindow::adjustScaledPixmap()
  73. {
  74. QGraphicsScene *scene = new QGraphicsScene;
  75. QGraphicsPixmapItem *item = new QGraphicsPixmapItem(*pixPtr_);
  76. item->setTransformationMode(Qt::SmoothTransformation);
  77. scene->addItem(item);
  78. ui->graphicsViewMap->setScene(scene);
  79. ui->graphicsViewMap->fitInView(scene->sceneRect(), Qt::IgnoreAspectRatio);
  80. }
Add Comment
Please, Sign In to add comment