QT Primer-list box Qlistwidget class

Source: Internet
Author: User
Tags sort

The Qlistwidget class is also a common class in the GUI, and it derives from the Qlistview:

Class Q_gui_export Qlistwidget:public qlistview
{
    q_object


Common Methods and properties:

(1) AddItem

void AddItem (const QString & label)
void AddItem (Qlistwidgetitem * Item)
void Additems (const qstringlist & Labels)

Add one or more items.

(2)

int count () const

The total number of list items that contain hidden list items.

(3) CurrentItem

Qlistwidgetitem * CurrentItem () const

Returns the currently selected item.

(4) CurrentRow

int CurrentRow () const

Returns the ordinal of the currently selected item.

(5) InsertItem

void InsertItem (int row, Qlistwidgetitem * Item)
void InsertItem (int row, const QString & label)
void Insertitems (int row, const qstringlist & Labels)

Inserts a new item into the list box.

(6)

Qlistwidgetitem * Item (int row) const

Returns the list item that is ordinal to row.

(7) Removeitemwidget

void Removeitemwidget (Qlistwidgetitem * Item)

Deletes the specified item item.

(8) SelectedItems

Qlist<qlistwidgetitem *> SelectedItems () const

Returns the currently selected item.

(9)

void Setcurrentitem (Qlistwidgetitem * Item)

Sets the currently selected item to item.

(10)

void Setcurrentrow (int row)

Sets the current selection.

(11)

void setsortingenabled (bool enable)

Sets whether the sort feature is enabled.

(12)

void Sortitems (Qt::sortorder order = qt::ascendingorder)

To sort a list box, the value of the parameter can be:

Qt::ascendingorder 0 The items is sorted ascending e.g. starts with ' AAA ' ends with ' ZZZ ' in La Tin-1 locales
Qt::D Escendingorder 1 The items is sorted descending e.g. starts with ' ZZZ ' ends with ' AAA ' in Lat In-1 locales
(13)

Qlistwidgetitem * Qlistwidget::takeitem (int row)

Deletes an item.

Example:

Window.h

#ifndef __window_h__ #define __WINDOW_H__ #include <QMainWindow> #include <QPushButton> #include <
qlineedit> #include <QLayout> #include <QLabel> #include <QComboBox> #include <QMessageBox> #include <QDialog> #include <QListWidget> class window:public Qmainwindow {q_object Public:window (qwi
		Dget *parent = NULL): Qmainwindow (parent) {listwidget = new Qlistwidget (this);
		New Qlistwidgetitem (tr ("Oak"), Listwidget);
		New Qlistwidgetitem (tr ("Fir"), Listwidget);

		
		New Qlistwidgetitem (tr ("Pine"), Listwidget);
		Qlabel *lbl = new Qlabel (tr ("Select text:"));
		
		EDT = new Qlineedit;
		
		Qvboxlayout *leftlayout = new Qvboxlayout;
		Leftlayout->addwidget (Listwidget);
		Leftlayout->addwidget (LBL);

		Leftlayout->addwidget (EDT);
		Edt_add = new Qlineedit;
		Qpushbutton *add_btn = new Qpushbutton (tr ("add"));
		Qpushbutton *del_btn = new Qpushbutton (tr ("Del")); Qpushbutton *quit_btn = new Qpushbutton (tr ("quit"));
		Qvboxlayout *rightlayout = new Qvboxlayout;
		Rightlayout->addwidget (Edt_add);
		Rightlayout->addwidget (ADD_BTN);
		Rightlayout->addwidget (DEL_BTN);
		Rightlayout->addstretch ();

		Rightlayout->addwidget (QUIT_BTN);
		Qhboxlayout *mainlayout = new Qhboxlayout;
		Mainlayout->addlayout (leftlayout);

		Mainlayout->addlayout (rightlayout);
		Qwidget *mainwidget = new Qwidget;

		Mainwidget->setlayout (mainlayout);

		Setcentralwidget (Mainwidget);
		Connect (Listwidget, SIGNAL (itemselectionchanged ()), this, SLOT (itemselectionchanged ()));
		Connect (add_btn, SIGNAL (clicked ()), this, SLOT (On_add ()));
		Connect (del_btn, SIGNAL (clicked ()), this, SLOT (On_del ()));
	Connect (quit_btn, SIGNAL (clicked ()), this, SLOT (Close ()));
	} Private:qlistwidget *listwidget;

Qlineedit *edt, *edt_add;
	Private Slots:void itemselectionchanged () {Edt->settext (Listwidget->currentitem ()->text ()); } void On_add () {Listwidget->insertitem (listwidget->currentrow() + 1, edt_add->text ());
	} void On_del () {Listwidget->takeitem (Listwidget->currentrow ());


}


};
 #endif


Main.cpp

#include <QApplication>
#include <QDialog>
#include "Window.h"



int main (int argc, char *argv[ ])
{
	qapplication A (argc, argv);
	Window *mainwindow = new window;



	Mainwindow->resize (a);
	Mainwindow->setwindowtitle (Qwidget::tr ("Qt Test"));
	Mainwindow->show ();

	return a.exec ();
}


Compile and run with the following interface:

Qlistwidget items can be displayed with a picture, example:

    Qlistwidgetitem *configbutton = new Qlistwidgetitem (contentswidget);
    Configbutton->seticon (Qicon (":/images/config.png"));
    Configbutton->settext (tr ("Configuration"));
    Configbutton->settextalignment (qt::alignhcenter);
    Configbutton->setflags (qt::itemisselectable | qt::itemisenabled);

    Qlistwidgetitem *updatebutton = new Qlistwidgetitem (contentswidget);
    Updatebutton->seticon (Qicon (":/images/update.png"));
    Updatebutton->settext (tr ("Update"));
    Updatebutton->settextalignment (qt::alignhcenter);
    Updatebutton->setflags (qt::itemisselectable | qt::itemisenabled);

    Qlistwidgetitem *querybutton = new Qlistwidgetitem (contentswidget);
    Querybutton->seticon (Qicon (":/images/query.png"));
    Querybutton->settext (tr ("Query"));
    Querybutton->settextalignment (qt::alignhcenter);
    Querybutton->setflags (qt::itemisselectable | qt::itemisenabled);

As shown in figure:



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.