QT commonly used basic control usage arrangement

Qt commonly used basic control usage arrangement (only for learning records)

1. QLabel tags, used to display text, images and other content

The above are all the functions borrowed from the Qt assistant. Of course, the most commonly used and important ones are as follows:

void setText(QString); //Set the text in the label box. 
void hide(); //Hide the label box. 
void setBuddy(QWidget*); //Set another component as a partner of the label box, which is convenient and quick to use. 
void clear(); //Clear all content in the label box. 
void setPixmap(QPixmap(QString)); //Set the picture. 
void setScaledContents(bool); //Set whether to fill the entire label box proportionally (very important) 
void setToolTip(QString); //Set the information prompt, the mouse will automatically jump out of the text when placed on the label box. 
void setAlignment(Qt::Alignment); //Set the alignment format of the label box. 
void setStyleSheet(QString); //Set the label box style

2. The QPushButton button control is used to trigger the response event

Of course, the most commonly used and important ones are as follows:

void setText(QString); 
void setText(const QString &text); 
QString text() const; 
void setIcon(const QIcon &icon);
QIcon icon() const; 
QSize iconSize() const; 
void setCheckable(bool); 
bool isCheckable() const; 
bool isChecked() const; 
void setDown(bool); 
bool isDown() const; 
void setAutoRepeat(bool); 

usage:

Button creation:

QPushButton * btn = new QPushButton;

set parent window

btn -> setParent(this);

display text 

btn ->setText("Zhang Fei");

move 

btn ->move(x,y);

set a fixed size window

setFixedSize(width, height);

Signals: clicked(), pressed()

3. The QLineEdit edit box represents a single-line text edit control

Common slot functions:

void clear() //clear content
void copy() const //copy content
void cut() //cut content
void paste()//paste content
void redo() //restore
void selectAll() //select all content
void setText(const QString &) //Set content
void undo() //Cancel

Commonly used signals:

1.void cursorPositionChanged(int old, int new); //When the cursor position changes, a signal is found.
2.void returnPressed(); //When the cursor is in the line edit box, click Enter to send a signal.
3.void selectionChanged( ) //Send a signal when the selected text changes.
4.void textChanged(const QString & text) //Send a signal when the text changes.

Four,QRadioButton is a radio button, when there are multiple QRadioButton controls, only one can be selected

Note: Use the QGroupBox control for grouping, which usually has a border and a title bar, and is used as a container component; it should be noted that the layout control (QLayout) must be used inside the QGroupBox container for layout

Step 1: Create a new group: QButtonGroup* pBgGroup = new QButtonGroup(this);

Step 2: Add the QRadioButton object that exists on the ui or the QRadioButton object created by yourself to bgGroup

pBgGroup->addButton( ui->rabutton_0 ,0);
pBgGroup->addButton( ui->rabutton_1 ,1);
pBgGroup->addButton( ui->rabutton_2 ,2);
ui->rabutton_0->setChecked(true);

Step 3: Add the association of signals and slots

connect( pBgGroup, SIGNAL(buttonToggled(int, bool), this, SLOT(on_bgCurGroup_toggled(int, bool)));

connect( ui->rabutton_0, &QRadioButton::toggled, this, SLOT(on_bgClick_toggled(bool)));

Step 4: Slot function implementation

void RadioButtonGroup::on_bgCurGroup_toggled(int id, bool status) 

  qDebug() << pBgGroup->checkedId(); //组id
  qDebug() << QString::number(id); //QRadioButton id
  qDebug() << QString::number(status); //check status
}

void RadioButtonGroup::on_bgClick_toggled(bool isChecked) 

    if (isChecked == true)
        qDebug() << "The radio button is checked";
    else if (isChecked == false)
        qDebug() << "The radio button loses focus" ;
}

Five, QCheckBox check box, QCheckBox (check box) and QRadioButton (radio button) are option buttons, QButtonGroup can be used to visually organize many check boxes together

Signal function:
void stateChanged(int state)

Important inherited functions: text(), setText(), pixmap(), setPixmap(), accel(), setAccel(), isToggleButton(), setDown(), isDown(), isOn(), checkState(), autoRepeat (), isExclusiveToggle(), group(), setAutoRepeat(), toggle(), pressed(), released(), clicked(), toggled(), checkState(), stateChanged() public functions: Qt::

CheckState
checkState () const Returns the checked state of the checkbox. If you don't need tristate support, you can use QAbstractButton::isChecked(), which returns a Boolean
bool isTristate() const whether the checkbox is a tristate checkbox. (The default is false, which means that the check box has only two states)
void setCheckState(Qt::CheckState state) Set the checked state of the check box. If you don't need tristate support, you can use QAbstractButton:setChecked(), which accepts a Boolean void
setTristate(bool y = true) to set the checkbox as a tristate checkbox

Simple usage: add member variable m_pCurLabel in the header file

QCheckBox *pCurCheckBox = new QCheckBox(this);
m_pCurLabel = new QLabel(this);
 
m_pCurLabel->setText("click");
pCurCheckBox->setText(QString::fromLocal8Bit("three-state check box"));
 
// Open tristate mode
pCurCheckBox->setTristate();  
 
// connect signal slot
connect(pCurCheckBox, SIGNAL(stateChanged(int)), this, SLOT(onCurStateChanged(int)));

// slot function

void MainWindow::onCurStateChanged(int state)
{
    if (state == Qt::Checked) // "选中"
    {
        m_pCurLabel->setText("Checked");
    }
    else if(state == Qt::PartiallyChecked) // "半选"
    {
        m_pCurLabel->setText("PartiallyChecked");
    }
    else // 未选中 - Qt::Unchecked
    {
        m_pCurLabel->setText("Unchecked");
    }
}

6. The QListWidget list box control supports two list item display modes, namely QListView::IconMode and QListView::ListMode, and the commonly used add, delete, click, double-click operations and list item display mode settings of the list box

Common signal functions:

void currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
void currentRowChanged(int currentRow)
void currentTextChanged(const QString &currentText)
void itemActivated(QListWidgetItem *item)
void itemChanged(QListWidgetItem *item)
void itemClicked(QListWidgetItem *item)
void itemDoubleClicked(QListWidgetItem *item)
void itemEntered(QListWidgetItem *item)
void itemPressed(QListWidgetItem *item)
void itemSelectionChanged()

Simple usage:

QListWidget *listWidget = new QListWidget(this);

 //Do not add icons
 listWidget->addItem(new QListWidgetItem(tr("Line"))); 
 listWidget->addItem(new QListWidgetItem(tr("Rectangle"))); 
 listWidget->addItem(new QListWidgetItem(tr(" Oval"))); 
 listWidget->addItem(new QListWidgetItem(tr("Triangle")));

//加图标
 listWidget->addItem(new QListWidgetItem(QIcon(":/images/line.PNG"), tr("Line"))); 
 listWidget->addItem(new QListWidgetItem(QIcon(":/images/rect.PNG"), tr("Rectangle"))); 
 listWidget->addItem(new QListWidgetItem(QIcon(":/images/oval.PNG"), tr("Oval"))); 
 listWidget->addItem(new QListWidgetItem(QIcon(":/images/tri.PNG"), tr("Triangle")));

7. The QListView control can be used to display data in the form of a list. In Qt, the model/View structure is used to manage the relationship between data and views. The model is responsible for data access, and the data interaction is realized through the delegate.

QT provides some ready-made models for handling data items:
QStringListModel is used to store simple QString lists.
QStandardItemModel manages complex tree-structured data items, each of which can contain arbitrary data.
QDirModel provides file and directory information in the local file system.
QSqlQueryModel, QSqlTableModel, QSqlRelationTableModel are used to access the database

For custom data types, if you want to use QVariant, you must register with Q_DECLARE_METATYPE.

struct ItemsData{
    QString name;
    QString tel;
};

Q_DECLARE_METATYPE(ItemsData

Single data access
//save
Item->setData(itemStatus,Qt::UserRole); // single access

//取
ItemStatus status = (ItemStatus)(index.data(Qt::UserRole).toInt());

Structure data access
//Save
Item->setData(QVariant::fromValue(itemData),Qt::UserRole+1);//Overall access

//取
QVariant variant = index.data(Qt::UserRole+1);
ItemData datas = variant.value<ItemData>();

Simple usage:

QStringList num;

num<<QString("1")<<QString("2")<<QString("3")<<QString("4");

QStringListModel *model = new QStringListModel(num);

ui->listView->setModel(model);

Eight, QTreeWidget tree control

信号函数:
void currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)
void itemActivated(QTreeWidgetItem *item, int column)
void itemChanged(QTreeWidgetItem *item, int column)
void itemClicked(QTreeWidgetItem *item, int column)
void itemCollapsed(QTreeWidgetItem *item)
void itemDoubleClicked(QTreeWidgetItem *item, int column)
void itemEntered(QTreeWidgetItem *item, int column)
void itemExpanded(QTreeWidgetItem *item)
void itemPressed(QTreeWidgetItem *item, int column)
void itemSelectionChanged()

槽函数:
void clear()
void collapseItem(const QTreeWidgetItem *item)
void expandItem(const QTreeWidgetItem *item)
void scrollToItem(const QTreeWidgetItem *item, QAbstractItemView::ScrollHint hint = EnsureVisible)
简单用法:
QTreeWidget *treeWidget = new QTreeWidget();
treeWidget->setColumnCount(1);
QList<QTreeWidgetItem *> items;
for (int i = 0; i < 10; ++i)
    items.append(new QTreeWidgetItem((QTreeWidget*)0, QStringList(QString("item: %1").arg(i))));
treeWidget->insertTopLevelItems(0, items);

//Expand
treeWidget->expandAll();

Adding a CheckBox to a node
It is relatively simple to add a CheckBox to a tree node, as long as it is set, it can be displayed
pItem1->setCheckState(0,Qt::Unchecked);
pItem2->setCheckState(0,Qt::PartiallyChecked);
pItem3-> setCheckState(0, Qt::checked);

// node add child node
pItem->addChild(..);

//Hiding the header
Whether the header is needed, the hiding is controlled by the setHeaderHidden method:
setHeaderHidden(true)

// Clear the original data
treeWidget->clear();

//Shrink
treeWidget->collapseAll();

Nine, QTreeView control

Slot function:

void collapse(const QModelIndex &index)
void collapseAll()
void expand(const QModelIndex &index)
void expandAll()
void expandToDepth(int depth)
void hideColumn(int column)
void resizeColumnToContents(int column)
void showColumn(int column)
信号函数:
void collapsed(const QModelIndex &index)
void expanded(const QModelIndex &index)

model
QT provides some ready-made models for handling data items:
QStringListModel is used to store simple QString lists.
QStandardItemModel manages complex tree-structured data items, each of which can contain arbitrary data.
QDirModel provides file and directory information in the local file system. QSqlQueryModel, QSqlTableModel, QSqlRelationTableModel are used to access the database.

Note: To enable a QTreeView to display data, a model needs to be constructed and set to the QTreeView. Qt provides some related types of Models, the
most commonly used of which is the QStandardItemModel class, which can basically meet most needs. The content of the header is also managed by this model.
The setHorizontalHeaderLabels function can set the number of columns and the text in each column. The first-level nodes are directly added to the model using the appendRow method
, and the secondary nodes are added to the first parent node to form a parent-child relationship tree in turn.

Simple way:

void MainWindow::InitCurrentTree()
{     //1, construct Model, here is an example of the model construction process with 3-layer relationship     QStandardItemModel* model = new QStandardItemModel(ui->treeView);     model->setHorizontalHeaderLabels(QStringList()<<QStringLiteral( "Number") << QStringLiteral("Name")); //Set column header     for(int i=0;i<10;i++)     {         //First-level node, add mModel         QList<QStandardItem*> items1;         QStandardItem* item1 = new QStandardItem(QString::number(i));         QStandardItem* item2 = new QStandardItem(QStringLiteral("first-level node"));         items1.append(item1);         items1.append(item2);         model->appendRow(items1);         for(int j=0;j<10;j++)         {












 


            //Second-level node
            QList<QStandardItem*> items2;
            QStandardItem* item3 = new QStandardItem(QString::number(j));
            QStandardItem* item4 = new QStandardItem(QStringLiteral("Second-level node"));
            items2.append( item3);
            items2.append(item4);
            item1->appendRow(items2);
        }
    }
    //2, apply model
    ui->treeView->setModel(model) to QTreeView;
}

Ten, QTableWidget control

信号函数:
void cellActivated(int row, int column)
void cellChanged(int row, int column)
void cellClicked(int row, int column)
void cellDoubleClicked(int row, int column)
void cellEntered(int row, int column)
void cellPressed(int row, int column)
void currentCellChanged(int currentRow, int currentColumn, int previousRow, int previousColumn)
void currentItemChanged(QTableWidgetItem *current, QTableWidgetItem *previous)
void itemActivated(QTableWidgetItem *item)
void itemChanged(QTableWidgetItem *item)
void itemClicked(QTableWidgetItem *item)
void itemDoubleClicked(QTableWidgetItem *item)
void itemEntered(QTableWidgetItem *item)
void itemPressed(QTableWidgetItem *item)
void itemSelectionChanged()

Slot function:

void clear()
void clearContents()
void insertColumn(int column)
void insertRow(int row)
void removeColumn(int column)
void removeRow(int row)
void scrollToItem(const QTableWidgetItem *item, QAbstractItemView::ScrollHint hint = EnsureVisible)

Simple usage:

 QTableWidget *tableWidget = new QTableWidget;
 tableWidget->setRowCount(8); //Set the number of rows to 10
 tableWidget->setColumnCount(4); //Set the number of columns to 5
 tableWidget->resize(450, 200); //Set Table
 QStringList header;
 header<<"ID"<<"Name";
 tableWidget->setHorizontalHeaderLabels(header);
 tableWidget->setItem(0,0,new QTableWidgetItem("0001"));
 tableWidget->setItem(1,0 ,new QTableWidgetItem("0002"));
 tableWidget->setItem(2,0,new QTableWidgetItem("0003"));

 tableWidget->setItem(0,1,new QTableWidgetItem(QIcon(":/Image/month.png"), "John"));
 tableWidget->setItem(1,1,new QTableWidgetItem(QIcon(":/Image/month.png"), "Tom"));
 tableWidget->setItem(2,1,new QTableWidgetItem(QIcon(":/Image/month.png"), "Lucy"));

The realization of the effect of merging cells:
tableWidget->setSpan(0, 0, 3, 1)

Match the size of rows and columns to the content
tableWidget->resizeColumnsToContents(); 
tableWidget->resizeRowsToContents(); 

Set the table to select the entire row
tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); //The way the entire row is selected

 

 

Guess you like

Origin blog.csdn.net/leiyang2014/article/details/115435468