Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Custom QMainWindow

Custom QMainWindow

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 3.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    beowulf
    wrote on last edited by
    #1

    Hi guys, i'm trying to make a custom window, i can do this using this code, but i can't resize because of the fixed size, and i can't move only in the Titlebar (custom title bar), in any place i can move, how move only in the titlebar?.

    MyWindow.h
    @#include <QMainWindow>
    #include <QLabel>
    #include <QPushButton>
    #include <QMouseEvent>

    class MyWindow : public QMainWindow {
    Q_OBJECT
    private:
    QLabel *label_title,*label_quit;
    QPushButton *bn_exit;
    QPixmap *pixmap;
    QPoint m_dragPosition;

    public:
    MyWindow(QMainWindow *parent = 0, Qt::WindowFlags flags = 0);
    protected:
    void paintEvent(QPaintEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void mousePressEvent(QMouseEvent *event);
    };@

    MyWindow.cpp
    @#include "MyWindow.h"
    #include <QApplication>
    #include <QPainter>
    #include <QLabel>

    MyWindow::MyWindow(QMainWindow *parent, Qt::WindowFlags flags) : QMainWindow(parent,flags) {

    resize(1024,576);
    setWindowTitle("Versuch1");
    
    //Titel
    label_title = new QLabel("irgendwas",this);
    label_title->setGeometry(10,10,500,40);
    label_title->setStyleSheet("font-family: Arial; letter-spacing: 4px;font-weight:bold; color : white;font-size: 30px");
    
    //Exit-Button
    bn_exit = new QPushButton("[Exit]",this);
    bn_exit->setGeometry(975,10,40,20);
    bn_exit->setStyleSheet("QPushButton {font-family: Verdana; font-size:15px; top:0px; border: none; background-color:#101010; color:white} QPushButton:hover {color: red }");
    connect(bn_exit,SIGNAL(clicked()),qApp,SLOT(quit()));
    

    }

    void MyWindow::paintEvent(QPaintEvent *event) // Painter
    {
    QPainter painter(this);
    painter.setPen(Qt::NoPen); // deactivate Border

    painter.setBrush(QBrush("#101010")); // black title and footer
    painter.drawRect(0, 0, 1024, 60);

    painter.setBrush(QBrush("#101010"));
    painter.drawRect(0, 516, 1024, 576);
    }
    void MyWindow::mouseMoveEvent(QMouseEvent *event)
    {
    if (event->buttons() & Qt::LeftButton) {
    move(event->globalPos() - m_dragPosition);
    event->accept();
    }
    }
    void MyWindow::mousePressEvent(QMouseEvent *event)
    {
    if (event->button() == Qt::LeftButton) {
    m_dragPosition = event->globalPos() - frameGeometry().topLeft();
    event->accept();
    }
    }@

    main.cpp
    @#include <QApplication>
    #include "MyWindow.h"

    int main(int argc, char argv[]) {
    QApplication app(argc, argv);
    MyWindow
    window = new MyWindow(0, Qt::FramelessWindowHint);
    window->show();
    return app.exec();
    }@

    -- 0x00

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      when i got you right you want to move the main window with the "label_title":
      @
      label_title->installEventFilter(this);
      ...
      bool MyWindow::eventFilter(QObject* watched, QEvent* event)
      {
      if( watched == label_title )
      {
      switch( event->type() )
      {
      case QEvent::MousePress:
      {
      QMouseEvent* me = static_cast<QMouseEvent*>(event);
      if( me->button() == Qt::LeftButton )
      {
      m_MovingState = true;
      m_MovingPos = me->globalPos();
      }
      }
      break;
      case QEvent::MouseMove:
      {
      if( m_MovingState )
      {
      QMouseEvent* me = static_cast<QMouseEvent*>(event);
      m_State = Moving;
      this->move( this->pos() + (me->globalPos() - m_MovingPos) );
      m_MovingPos = me->globalPos();
      }
      }
      break;
      case QEvent::MouseRelease:
      {
      QMouseEvent* me = static_cast<QMouseEvent*>(event);
      if( me->button() == Qt::LeftButton )
      m_MovingState = false;
      }
      break;
      }
      }
      }
      @

      Not tested though...but it should give you an idea..

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved