Qt Designer to write custom controls - Improvement Act (lift method)

Upgrade (if you call a custom class control) controls in Qt class
https://blog.csdn.net/qq_41399894/article/details/92803923
QT programming: Qt Extended control method
https://blog.csdn.net/ jekcai / article / details / 80436581

Upgrade (if you call a custom class controls) Qt in control class

A new space projects, adding pushbutton, right-click on the file in the UI to enhance the class, appear:

At this time, no upgrade options for classes;

Next: add a new file -> a custom class that inherits With Pushbutton , as shown in the code:


  
  
  1. CMyPushButton.h in:
  2. #ifndef CMYPUSHBUTTON_H
  3. #define CMYPUSHBUTTON_H
  4. #include <QPushButton>
  5. #include <QMouseEvent>
  6. class CMyPushButton : public QPushButton
  7. {
  8. public:
  9. CMyPushButton(QWidget *parent= 0);
  10. protected:
  11. void mousePressEvent (QMouseEvent * s) ;
  12. };
  13. #endif // CMYPUSHBUTTON_H
  14. CMyPushButton.c in:
  15. #include "cmypushbutton.h"
  16. #include "QDebug"
  17. CMyPushButton::CMyPushButton(QWidget *parent):QPushButton(parent)
  18. {
  19. }
  20. void CMyPushButton::mousePressEvent(QMouseEvent * e)
  21. {
  22. if(e->button()==Qt::LeftButton)
  23. {
  24. qDebug()<< "leftbutton clicked";
  25. }
  26. }

All files:

In addition to mainwindow.ui a button control, nothing else, enter mainwindow.ui

Click promoted -> Enter the name of the custom class:

Click Add:

Check, click on commission, after generating it, and now look at the effect of:

Class name will change accordingly after a successful

Click to see the emergence of a print information CMyPushButton class, but it is a window MainWindow

QT Programming: Qt Extended Control Methods

Qt Extended Controls:

       Qt comes with controls, generally unable to meet some custom operations. For example QGraphicsView, native can not respond to mouse wheel events zoom solid line, the same can not be dragged through the right mouse button. Corresponding scroll bar can be scrolled in response to the mouse wheel. Here this can be achieved by extending the native QGraphicsView control and shield the mouse wheel scroll bar operation. There are similar controls, QListView, achieve clickable elements may be issued in response to a signal corresponding to the groove.

Extension method:

  • Step one: the need to write your own control class that inherits from Qt native control classes, such as:

  
  
  1. class MyGraphicsViewpublic QGraphicsView
  2. {
  3. Q_OBJECT
  4. public:
  5. explicit MyGraphicsView(QWidget* parent = 0);
  6. virtual ~MyGraphicsView();
  7. protected:
  8. virtual void mousePressEvent(QMouseEvent* event);
  9. virtual void mouseMoveEvent(QMouseEvent* event);
  10. virtual void mouseReleaseEvent(QMouseEvent* event);
  11. virtual void wheelEvent(QWheelEvent* event);
  12. }

      And override the methods, implement the functions they need. It can also be extended to achieve functionality they need.

  • Step 2: Open mainwindow.ui layout file, you need to select the extended control, right-click on it:

      Then click the "upgraded" button:

       For example: here I need to extend QTextEdit control, I need to add my name to the class written "to enhance the class name" column, the corresponding class header file based on the class name is automatically added to the "header" column . It should be noted, the header file name of the class and the class name needs to be consistent, try to use to create classes provided by Qt automatically create files.

       Finally, above: Check the class to add, and click on the "upgrade" button. The corresponding controls will increase as a custom type:

 

Previously used the wrong way to compare:

       Qt layout file corresponding to the class, will be re-generated when re-building, but in the course of each run, they will not be changed (and possible ways to compile Qt related) Therefore, the file can be found in mainwindow.cpp ui_mainwindow.h program entry:

May be added to the MainWindow constructor:

ui->graphicsView = new MyGraphicsView;
  
  

Then find an in ui_mainwindow.h in:

graphicsView = new QGraphicsView(centralWidget);
  
  

And comment it out. And maintained after each re-build, came in the ui_mainwindow.h file comment it out. The same can achieve the purpose of rewriting the native control.

The principle is, C ++ class hierarchy downcast, parent class pointer can point to an object subclass. And call the parent class method of rewriting away. This alias syntax is: dynamic binding.

But more unfortunately, can not be used to expand out of their own is different from the new parent class. This is because, although the parent class pointer can point to the object subclasses, but can not call out new ways to expand the subclass. The syntax alias called: static binding.

So the above method, but the method is not the whole expansion project editor Qt controls should be used can solve part of the problem.

Published 42 original articles · won praise 148 · views 410 000 +

Guess you like

Origin blog.csdn.net/baidu_37503452/article/details/104539515